1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-12 10:14:02 +02:00

Customize get dirty paths (#5069)

* Move getDirtyPaths to the Editor object to allow for customization

* Add changeset for getDirtyPaths customization

Co-authored-by: Tom Krenzke <tom@daybreakhealth.com>
This commit is contained in:
krenzke
2022-08-17 23:54:21 -07:00
committed by GitHub
parent 82a10a38e6
commit 46d113fe1e
5 changed files with 86 additions and 77 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': patch
---
Expose getDirtyPaths method on Editor object to allow for customization

View File

@@ -71,7 +71,7 @@ export const createEditor = (): Editor => {
dirtyPathKeys = oldDirtyPathKeys dirtyPathKeys = oldDirtyPathKeys
} }
const newDirtyPaths = getDirtyPaths(op) const newDirtyPaths = editor.getDirtyPaths(op)
for (const path of newDirtyPaths) { for (const path of newDirtyPaths) {
add(path) add(path)
} }
@@ -296,16 +296,12 @@ export const createEditor = (): Editor => {
} }
} }
}, },
}
return editor /**
}
/**
* Get the "dirty" paths generated from an operation. * Get the "dirty" paths generated from an operation.
*/ */
const getDirtyPaths = (op: Operation): Path[] => { getDirtyPaths: (op: Operation): Path[] => {
switch (op.type) { switch (op.type) {
case 'insert_text': case 'insert_text':
case 'remove_text': case 'remove_text':
@@ -375,4 +371,8 @@ const getDirtyPaths = (op: Operation): Path[] => {
return [] return []
} }
} }
},
}
return editor
} }

View File

@@ -77,6 +77,7 @@ export interface BaseEditor {
insertNode: (node: Node) => void insertNode: (node: Node) => void
insertText: (text: string) => void insertText: (text: string) => void
removeMark: (key: string) => void removeMark: (key: string) => void
getDirtyPaths: (op: Operation) => Path[]
} }
export type Editor = ExtendedType<'Editor', BaseEditor> export type Editor = ExtendedType<'Editor', BaseEditor>
@@ -626,6 +627,7 @@ export const Editor: EditorInterface = {
typeof value.normalizeNode === 'function' && typeof value.normalizeNode === 'function' &&
typeof value.onChange === 'function' && typeof value.onChange === 'function' &&
typeof value.removeMark === 'function' && typeof value.removeMark === 'function' &&
typeof value.getDirtyPaths === 'function' &&
(value.marks === null || isPlainObject(value.marks)) && (value.marks === null || isPlainObject(value.marks)) &&
(value.selection === null || Range.isRange(value.selection)) && (value.selection === null || Range.isRange(value.selection)) &&
Node.isNodeList(value.children) && Node.isNodeList(value.children) &&

View File

@@ -20,6 +20,7 @@ export const input = {
normalizeNode() {}, normalizeNode() {},
onChange() {}, onChange() {},
removeMark() {}, removeMark() {},
getDirtyPaths() {},
} }
export const test = value => { export const test = value => {
return Element.isElement(value) return Element.isElement(value)

View File

@@ -21,6 +21,7 @@ export const input = [
normalizeNode() {}, normalizeNode() {},
onChange() {}, onChange() {},
removeMark() {}, removeMark() {},
getDirtyPaths() {},
}, },
] ]
export const test = value => { export const test = value => {