Skip to content

Commit

Permalink
fix: support multiple-editors
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushAgrawal-A2 committed Dec 28, 2023
1 parent 214616d commit b5c24c9
Show file tree
Hide file tree
Showing 52 changed files with 545 additions and 198 deletions.
21 changes: 21 additions & 0 deletions packages/block-std/src/event/control/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export class ClipboardControl {
}

private _cut = (event: ClipboardEvent) => {
// if (
// !event.target ||
// !this._dispatcher.host.contains(event.target as Node)
// ) {
// return;
// }

const clipboardEventState = new ClipboardEventState({
event,
});
Expand All @@ -26,6 +33,13 @@ export class ClipboardControl {
};

private _copy = (event: ClipboardEvent) => {
// if (
// !event.target ||
// !this._dispatcher.host.contains(event.target as Node)
// ) {
// return;
// }

const clipboardEventState = new ClipboardEventState({
event,
});
Expand All @@ -36,6 +50,13 @@ export class ClipboardControl {
};

private _paste = (event: ClipboardEvent) => {
// if (
// !event.target ||
// !this._dispatcher.host.contains(event.target as Node)
// ) {
// return;
// }

const clipboardEventState = new ClipboardEventState({
event,
});
Expand Down
18 changes: 18 additions & 0 deletions packages/block-std/src/event/control/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export class RangeControl {
};

private _selectionChange = (event: Event) => {
// if (!this._checkSelectionSource()) {
// this._dispatcher.std.selection.clear();
// return;
// }

const scope = this._buildScope('selectionChange');

this._dispatcher.run('selectionChange', this._createContext(event), scope);
Expand All @@ -64,6 +69,19 @@ export class RangeControl {
return UIEventStateContext.from(new UIEventState(event));
}

private _checkSelectionSource = () => {

Check failure on line 72 in packages/block-std/src/event/control/range.ts

View workflow job for this annotation

GitHub Actions / Unit test

'_checkSelectionSource' is declared but its value is never read.
const selection = document.getSelection();
if (!selection || !selection.rangeCount) return true;

const range = selection.getRangeAt(0);
if (!range) return true;

if (!this._dispatcher.std.host.contains(range.startContainer)) return false;
if (!this._dispatcher.std.host.contains(range.endContainer)) return false;

return true;
};

private _buildScope = (eventName: EventName) => {
let scope: EventScope | undefined;
const selection = document.getSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
const inlineEditor = _getInlineEditor();
const inilneRange = inlineEditor.getInlineRange();
assertExists(inilneRange);
handleIndent(model.page, model, inilneRange.index);
handleIndent(blockElement.host, model, inilneRange.index);
_preventDefault(ctx);

return true;
Expand All @@ -271,7 +271,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
})
).selectedModels;
if (!models) return;
handleMultiBlockIndent(blockElement.page, models);
handleMultiBlockIndent(blockElement.host, models);
return true;
},
'Mod-Backspace': ctx => {
Expand All @@ -298,7 +298,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
const inlineRange = inlineEditor.getInlineRange();
assertExists(inlineRange);
if (inlineRange.index === 0) {
handleRemoveAllIndent(model.page, model, inlineRange.index);
handleRemoveAllIndent(blockElement.host, model, inlineRange.index);
_preventDefault(ctx);
}

Expand All @@ -311,7 +311,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
})
).selectedModels;
if (!models) return;
handleRemoveAllIndentForMultiBlocks(blockElement.page, models);
handleRemoveAllIndentForMultiBlocks(blockElement.host, models);
return true;
},
'Shift-Tab': ctx => {
Expand All @@ -337,7 +337,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
const inlineEditor = _getInlineEditor();
const inlineRange = inlineEditor.getInlineRange();
assertExists(inlineRange);
handleUnindent(model.page, model, inlineRange.index);
handleUnindent(blockElement.host, model, inlineRange.index);
_preventDefault(ctx);

return true;
Expand All @@ -349,7 +349,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
})
).selectedModels;
if (!models) return;
handleMultiBlockOutdent(blockElement.page, models);
handleMultiBlockOutdent(blockElement.host, models);
return true;
},
Backspace: ctx => {
Expand Down Expand Up @@ -464,7 +464,11 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
title: pageName,
})
.then(page => {
insertLinkedNode({ model: blockElement.model, pageId: page.id });
insertLinkedNode({
editorHost: blockElement.host,
model: blockElement.model,
pageId: page.id,
});
})
.catch(e => console.error(e));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function hardEnter(
// After
// - line1
// - | <-- will unindent the block
handleUnindent(page, model, range.index);
handleUnindent(editorHost, model, range.index);
return KEYBOARD_PREVENT_DEFAULT;
}

Expand All @@ -99,7 +99,7 @@ export function hardEnter(
if (matchFlavours(model, ['affine:code'])) {
if (shortKey) {
// shortKey+Enter to exit the block
handleBlockEndEnter(page, model);
handleBlockEndEnter(editorHost, model);
return KEYBOARD_PREVENT_DEFAULT;
}

Expand All @@ -126,12 +126,12 @@ export function hardEnter(
// - line1
// - | <-- will unindent the block
model.text.delete(range.index - 1, 1);
handleBlockEndEnter(page, model);
handleBlockEndEnter(editorHost, model);
return KEYBOARD_PREVENT_DEFAULT;
}

if (isEnd || shortKey) {
handleBlockEndEnter(page, model);
handleBlockEndEnter(editorHost, model);
return KEYBOARD_PREVENT_DEFAULT;
}

Expand All @@ -141,7 +141,7 @@ export function hardEnter(
return KEYBOARD_PREVENT_DEFAULT;
}

handleBlockSplit(page, model, range.index, range.length)?.catch(
handleBlockSplit(editorHost, model, range.index, range.length)?.catch(
console.error
);
return KEYBOARD_PREVENT_DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function tryConvertBlock(

const codeBlock = page.getBlockById(codeId);
assertExists(codeBlock);
asyncSetInlineRange(codeBlock, { index: 0, length: 0 }).catch(
asyncSetInlineRange(element.host, codeBlock, { index: 0, length: 0 }).catch(
console.error
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function convertToList(
});

const id = page.addBlock('affine:list', blockProps, parent, index);
asyncFocusRichText(page, id)?.catch(console.error);
asyncFocusRichText(element.host, page, id)?.catch(console.error);
}
return true;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ export function convertToParagraph(
});

const id = page.addBlock('affine:paragraph', blockProps, parent, index);
asyncFocusRichText(page, id)?.catch(console.error);
asyncFocusRichText(element.host, page, id)?.catch(console.error);
} else if (
matchFlavours(model, ['affine:paragraph']) &&
model['type'] !== type
Expand All @@ -94,7 +94,7 @@ export function convertToParagraph(
page.captureSync();

model.text?.delete(0, prefix.length + 1);
const inlineEditor = getInlineEditorByModel(model);
const inlineEditor = getInlineEditorByModel(element.host, model);
if (inlineEditor) {
inlineEditor.setInlineRange({
index: 0,
Expand Down Expand Up @@ -133,10 +133,12 @@ export function convertToDivider(

const nextBlock = parent.children[index + 1];
if (nextBlock) {
asyncFocusRichText(page, nextBlock.id)?.catch(console.error);
asyncFocusRichText(element.host, page, nextBlock.id)?.catch(
console.error
);
} else {
const nextId = page.addBlock('affine:paragraph', {}, parent);
asyncFocusRichText(page, nextId)?.catch(console.error);
asyncFocusRichText(element.host, page, nextId)?.catch(console.error);
}
}
return true;
Expand Down
Loading

0 comments on commit b5c24c9

Please sign in to comment.