-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from prezly/refactor/serialization
[MT-5076] Refactor - Move pre-serialization cleanup to Extension
- Loading branch information
Showing
24 changed files
with
145 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Node } from 'slate'; | ||
|
||
/** | ||
* Cleanup editor value before reporting it to the outer world. | ||
* Useful to clean up internal temporary nodes and properties. | ||
*/ | ||
export type Serialize = (nodes: Node[]) => Node[]; |
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
1 change: 0 additions & 1 deletion
1
packages/slate-editor/src/extensions/image/serialization/index.ts
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
packages/slate-editor/src/extensions/image/serialization/withoutImageCandidates.ts
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
packages/slate-editor/src/extensions/loader/serialization/index.ts
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
packages/slate-editor/src/extensions/loader/serialization/withoutLoaders.ts
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,6 @@ | ||
export { Editor } from './Editor'; | ||
export { createEditor } from './createEditor'; | ||
export { | ||
createEmptyValue, | ||
createEmptyEditorValueAsString, | ||
createEditorValueWithCoverageAsString, | ||
serialize, | ||
deserialize, | ||
isEditorValueEqual, | ||
} from './lib'; | ||
export type { RichBlocksAwareEditor } from './plugins'; | ||
export { createEmptyValue } from './lib'; | ||
export type { RichBlocksAwareEditor, SerializingEditor } from './plugins'; | ||
export type { EditorRef, EditorProps, Value } from './types'; | ||
export { useEditorEvents } from './useEditorEvents'; |
13 changes: 0 additions & 13 deletions
13
packages/slate-editor/src/modules/editor/lib/createEditorValueWithCoverageAsString.ts
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
packages/slate-editor/src/modules/editor/lib/createEmptyEditorValueAsString.ts
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
packages/slate-editor/src/modules/editor/lib/deserialize.ts
This file was deleted.
Oops, something went wrong.
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
81 changes: 77 additions & 4 deletions
81
packages/slate-editor/src/modules/editor/lib/isEditorValueEqual.test.ts
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 |
---|---|---|
@@ -1,10 +1,83 @@ | ||
import { deserialize } from './deserialize'; | ||
import { withoutNodes } from '@prezly/slate-commons'; | ||
import { isQuoteNode } from '@prezly/slate-types'; | ||
import { createEditor as createSlateEditor } from 'slate'; | ||
|
||
import { createEditor } from '#modules/editor'; | ||
|
||
import { isEditorValueEqual } from './isEditorValueEqual'; | ||
|
||
describe('slate-editor - isEditorValueEqual', () => { | ||
it('should return true for equivalent values', () => { | ||
const editor = createEditor(createSlateEditor(), () => []); | ||
|
||
const a = [ | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'Hello', bold: true }], | ||
}, | ||
]; | ||
const b = [ | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'Hello', bold: true }], | ||
}, | ||
]; | ||
|
||
expect(isEditorValueEqual(editor, a, b)).toBe(true); | ||
}); | ||
|
||
it('should return false for non-equivalent values', () => { | ||
const editor = createEditor(createSlateEditor(), () => []); | ||
|
||
const a = [ | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'Hello', bold: true }], | ||
}, | ||
]; | ||
const b = [ | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'hello', bold: true }], | ||
}, | ||
]; | ||
|
||
expect(isEditorValueEqual(editor, a, b)).toBe(false); | ||
}); | ||
|
||
it('should consider structural equality', () => { | ||
const a = '{ "type": "document", "children": [] }'; | ||
const b = '{ "children": [], "type": "document" }'; | ||
expect(isEditorValueEqual(deserialize(a), deserialize(b))).toBe(true); | ||
const editor = createEditor(createSlateEditor(), () => []); | ||
|
||
const a = [ | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'Hello', bold: true }], | ||
}, | ||
]; | ||
const b = [ | ||
{ | ||
children: [{ bold: true, text: 'Hello' }], | ||
type: 'paragraph', | ||
}, | ||
]; | ||
|
||
expect(isEditorValueEqual(editor, a, b)).toBe(true); | ||
}); | ||
|
||
it('should run pre-serialization cleanup from extensions', () => { | ||
const editor = createEditor(createSlateEditor(), () => [ | ||
{ | ||
id: 'PreSerializationCleanup', | ||
serialize: (nodes) => withoutNodes(nodes, isQuoteNode), | ||
}, | ||
]); | ||
|
||
const a = [ | ||
{ type: 'paragraph', children: [{ text: 'A wise man once said:' }] }, | ||
{ type: 'block-quote', children: [{ text: 'Hello' }] }, | ||
]; | ||
const b = [{ type: 'paragraph', children: [{ text: 'A wise man once said:' }] }]; | ||
|
||
expect(isEditorValueEqual(editor, a, b)).toBe(true); | ||
}); | ||
}); |
10 changes: 6 additions & 4 deletions
10
packages/slate-editor/src/modules/editor/lib/isEditorValueEqual.ts
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import type { Value } from '../types'; | ||
import type { Editor } from 'slate'; | ||
|
||
import { isEqual } from '#lodash'; | ||
|
||
import { serialize } from './serialize'; | ||
import type { Value } from '../types'; | ||
|
||
export function isEditorValueEqual(a: Value, b: Value): boolean { | ||
return a === b || serialize(a) === serialize(b); | ||
export function isEditorValueEqual(editor: Editor, a: Value, b: Value): boolean { | ||
return a === b || isEqual(editor.serialize(a), editor.serialize(b)); | ||
} |
This file was deleted.
Oops, something went wrong.
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.