-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added deleteBlocks method and fixed deleting several blocks (#181)
* added deleteBlocks method and fixed deleting several blocks after selection
- Loading branch information
1 parent
6031501
commit 34070d0
Showing
8 changed files
with
112 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { createDraft, finishDraft } from 'immer'; | ||
import { createEditor, Editor } from 'slate'; | ||
import { withHistory } from 'slate-history'; | ||
import { withReact } from 'slate-react'; | ||
import { buildBlockData } from '../../components/Editor/utils'; | ||
import { withShortcuts } from '../../extensions/shortcuts'; | ||
import { findPluginBlockBySelectionPath } from '../../utils/findPluginBlockBySelectionPath'; | ||
import { generateId } from '../../utils/generateId'; | ||
import { YooEditor, YooptaEditorTransformOptions } from '../types'; | ||
// // [TODO] Circular deps | ||
// import { buildSlateEditor } from '../../utils/editorBuilders'; | ||
|
||
function buildSlateEditor(editor: YooEditor): Editor { | ||
const slate = withShortcuts(editor, withHistory(withReact(createEditor()))); | ||
return slate; | ||
} | ||
|
||
export type DeleteBlocksOptions = Pick<YooptaEditorTransformOptions, 'focus' | 'focusAt' | 'slate'> & { | ||
blockIds?: string[]; | ||
paths?: number[]; | ||
deleteAll?: boolean; | ||
}; | ||
|
||
export function deleteBlocks(editor: YooEditor, options: DeleteBlocksOptions = {}) { | ||
const { deleteAll = false, paths, blockIds, focus = true } = options; | ||
|
||
if (deleteAll || Object.keys(editor.children).length === 1) { | ||
editor.children = {}; | ||
editor.blockEditorsMap = {}; | ||
const block = buildBlockData({ id: generateId() }); | ||
const slate = buildSlateEditor(editor); | ||
|
||
editor.children[block.id] = block; | ||
editor.blockEditorsMap[block.id] = slate; | ||
|
||
editor.setSelection([0]); | ||
editor.applyChanges(); | ||
editor.emit('change', editor.children); | ||
|
||
if (focus) editor.focusBlock(block.id, { waitExecution: true }); | ||
|
||
return; | ||
} | ||
|
||
if (Array.isArray(blockIds) && blockIds.length > 0) { | ||
editor.children = createDraft(editor.children); | ||
|
||
blockIds.forEach((id) => { | ||
delete editor.children[id]; | ||
delete editor.blockEditorsMap[id]; | ||
}); | ||
|
||
const blockDataIds = Object.keys(editor.children).sort((a, b) => | ||
editor.children[a].meta.order > editor.children[b].meta.order ? 1 : -1, | ||
); | ||
|
||
blockDataIds.forEach((id, index) => { | ||
editor.children[id].meta.order = index; | ||
}); | ||
|
||
editor.children = finishDraft(editor.children); | ||
editor.applyChanges(); | ||
editor.emit('change', editor.children); | ||
|
||
if (focus) editor.focusBlock(blockDataIds[0], { waitExecution: true }); | ||
return; | ||
} | ||
|
||
if (Array.isArray(paths) && paths.length > 0) { | ||
editor.children = createDraft(editor.children); | ||
|
||
paths.forEach((path) => { | ||
const block = findPluginBlockBySelectionPath(editor, { at: [path] }); | ||
if (block) { | ||
delete editor.children[block.id]; | ||
delete editor.blockEditorsMap[block.id]; | ||
} | ||
}); | ||
|
||
const blockDataIds = Object.keys(editor.children).sort((a, b) => | ||
editor.children[a].meta.order > editor.children[b].meta.order ? 1 : -1, | ||
); | ||
|
||
blockDataIds.forEach((id, index) => { | ||
editor.children[id].meta.order = index; | ||
}); | ||
|
||
editor.children = finishDraft(editor.children); | ||
editor.applyChanges(); | ||
editor.emit('change', editor.children); | ||
|
||
if (focus) editor.focusBlock(blockDataIds[0], { waitExecution: true }); | ||
|
||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.