Skip to content

Commit 3a5a653

Browse files
committed
Fixed text alignment with basic selection object
1 parent 4d44c84 commit 3a5a653

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

packages/core/src/BlockNoteEditor.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ToggledStyle,
3030
} from "./extensions/Blocks/api/inlineContentTypes";
3131
import { TextCursorPosition } from "./extensions/Blocks/api/cursorPositionTypes";
32+
import { Selection } from "./extensions/Blocks/api/selectionTypes";
3233
import { getBlockInfoFromPos } from "./extensions/Blocks/helpers/getBlockInfoFromPos";
3334
import {
3435
BaseSlashMenuItem,
@@ -259,6 +260,34 @@ export class BlockNoteEditor {
259260
}
260261
}
261262

263+
public getSelection(): Selection {
264+
const blocks: Block[] = [];
265+
266+
this._tiptapEditor.state.doc.descendants((node, pos) => {
267+
if (node.type.spec.group !== "blockContent") {
268+
return true;
269+
}
270+
271+
if (
272+
pos + node.nodeSize < this._tiptapEditor.state.selection.from ||
273+
pos > this._tiptapEditor.state.selection.to
274+
) {
275+
return true;
276+
}
277+
278+
blocks.push(
279+
nodeToBlock(
280+
this._tiptapEditor.state.doc.resolve(pos).node(),
281+
this.blockCache
282+
)
283+
);
284+
285+
return false;
286+
});
287+
288+
return { blocks: blocks };
289+
}
290+
262291
/**
263292
* Inserts new blocks into the editor. If a block's `id` is undefined, BlockNote generates one automatically. Throws an
264293
* error if the reference block could not be found.
@@ -282,7 +311,10 @@ export class BlockNoteEditor {
282311
* @param blockToUpdate The block that should be updated.
283312
* @param update A partial block which defines how the existing block should be changed.
284313
*/
285-
public updateBlock(blockToUpdate: Block, update: PartialBlock) {
314+
public updateBlock(
315+
blockToUpdate: BlockIdentifier | BlockIdentifier[],
316+
update: PartialBlock
317+
) {
286318
updateBlock(blockToUpdate, update, this._tiptapEditor);
287319
}
288320

@@ -311,7 +343,6 @@ export class BlockNoteEditor {
311343
public getActiveStyles() {
312344
const styles: Styles = {};
313345
const marks = this._tiptapEditor.state.selection.$to.marks();
314-
console.log(marks);
315346

316347
const toggleStyles = new Set<ToggledStyle>([
317348
"bold",

packages/core/src/api/blockManipulation/blockManipulation.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@ export function insertBlocks(
5757
}
5858

5959
export function updateBlock(
60-
blockToUpdate: BlockIdentifier,
60+
blockToUpdate: BlockIdentifier | BlockIdentifier[],
6161
update: PartialBlock,
6262
editor: Editor
6363
) {
64-
const id =
65-
typeof blockToUpdate === "string" ? blockToUpdate : blockToUpdate.id;
66-
const { posBeforeNode } = getNodeById(id, editor.state.doc);
64+
for (const block of Array.isArray(blockToUpdate)
65+
? blockToUpdate
66+
: [blockToUpdate]) {
67+
const id = typeof block === "string" ? block : block.id;
68+
const { posBeforeNode } = getNodeById(id, editor.state.doc);
6769

68-
editor.commands.BNUpdateBlock(posBeforeNode + 1, update);
70+
editor.commands.BNUpdateBlock(posBeforeNode + 1, update);
71+
}
6972
}
7073

7174
export function removeBlocks(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Block } from "./blockTypes";
2+
3+
export type Selection = {
4+
blocks: Block[];
5+
};

packages/react/src/FormattingToolbar/components/DefaultButtons/TextAlignButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const TextAlignButton = (props: {
2828
const setTextAlignment = useCallback(
2929
(textAlignment: DefaultBlockProps["textAlignment"]) => {
3030
props.editor.focus();
31-
props.editor.updateBlock(props.editor.getTextCursorPosition().block, {
31+
props.editor.updateBlock(props.editor.getSelection().blocks, {
3232
props: { textAlignment: textAlignment },
3333
});
3434
},

0 commit comments

Comments
 (0)