Skip to content

Commit

Permalink
feat: handle deleteContentBackward
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Mar 19, 2023
1 parent 55821dd commit b80970a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/src/components/editable/editable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ export class SlateEditableComponent implements OnInit, OnChanges, OnDestroy, Aft
if (nativeTargetRange) {
const targetRange = AngularEditor.toSlateRange(editor, nativeTargetRange);
if (data) {
setTimeout(() => {
restoreDom(editor, () => {
Transforms.insertText(editor, data.toString(), { at: targetRange });
}, 0);
});
} else {
restoreDom(editor, () => {
Transforms.delete(editor, { at: targetRange });
Expand Down
15 changes: 11 additions & 4 deletions packages/src/utils/restore-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EDITOR_TO_ELEMENT } from './weak-maps';

export function restoreDom(editor: Editor, execute: () => void) {
const editable = EDITOR_TO_ELEMENT.get(editor);
const observer = new MutationObserver(mutations => {
let observer = new MutationObserver(mutations => {
mutations.reverse().forEach(mutation => {
if (mutation.type === 'characterData') {
// We don't want to restore the DOM for characterData mutations
Expand All @@ -21,13 +21,20 @@ export function restoreDom(editor: Editor, execute: () => void) {
console.log('added: ', node);
});
});
observer.disconnect();
console.log('restore dom executed');
disconnect();
execute();
console.log('restore dom executed');
});
observer.observe(editable, { subtree: true, childList: true, characterData: true, characterDataOldValue: true });
setTimeout(() => {
const disconnect = () => {
observer.disconnect();
observer = null;
}
setTimeout(() => {
if (observer) {
disconnect();
execute();
}
}, 0);
}

Expand Down

0 comments on commit b80970a

Please sign in to comment.