@portabletext/editor 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/index.d.mts +911 -0
- package/lib/index.d.ts +911 -0
- package/lib/index.esm.js +4896 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.js +4874 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +4896 -0
- package/lib/index.mjs.map +1 -0
- package/package.json +119 -0
- package/src/editor/Editable.tsx +683 -0
- package/src/editor/PortableTextEditor.tsx +308 -0
- package/src/editor/__tests__/PortableTextEditor.test.tsx +386 -0
- package/src/editor/__tests__/PortableTextEditorTester.tsx +116 -0
- package/src/editor/__tests__/RangeDecorations.test.tsx +115 -0
- package/src/editor/__tests__/handleClick.test.tsx +218 -0
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +389 -0
- package/src/editor/__tests__/utils.ts +39 -0
- package/src/editor/components/DraggableBlock.tsx +287 -0
- package/src/editor/components/Element.tsx +279 -0
- package/src/editor/components/Leaf.tsx +288 -0
- package/src/editor/components/SlateContainer.tsx +81 -0
- package/src/editor/components/Synchronizer.tsx +190 -0
- package/src/editor/hooks/usePortableTextEditor.ts +23 -0
- package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +24 -0
- package/src/editor/hooks/usePortableTextEditorSelection.ts +22 -0
- package/src/editor/hooks/usePortableTextEditorValue.ts +16 -0
- package/src/editor/hooks/usePortableTextReadOnly.ts +20 -0
- package/src/editor/hooks/useSyncValue.test.tsx +125 -0
- package/src/editor/hooks/useSyncValue.ts +372 -0
- package/src/editor/nodes/DefaultAnnotation.tsx +16 -0
- package/src/editor/nodes/DefaultObject.tsx +15 -0
- package/src/editor/nodes/index.ts +189 -0
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +244 -0
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +142 -0
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +346 -0
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +162 -0
- package/src/editor/plugins/__tests__/withHotkeys.test.tsx +212 -0
- package/src/editor/plugins/__tests__/withInsertBreak.test.tsx +204 -0
- package/src/editor/plugins/__tests__/withPlaceholderBlock.test.tsx +133 -0
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +65 -0
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +1377 -0
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +91 -0
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +115 -0
- package/src/editor/plugins/createWithEditableAPI.ts +573 -0
- package/src/editor/plugins/createWithHotKeys.ts +304 -0
- package/src/editor/plugins/createWithInsertBreak.ts +45 -0
- package/src/editor/plugins/createWithInsertData.ts +359 -0
- package/src/editor/plugins/createWithMaxBlocks.ts +24 -0
- package/src/editor/plugins/createWithObjectKeys.ts +63 -0
- package/src/editor/plugins/createWithPatches.ts +274 -0
- package/src/editor/plugins/createWithPlaceholderBlock.ts +36 -0
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +91 -0
- package/src/editor/plugins/createWithPortableTextLists.ts +160 -0
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +441 -0
- package/src/editor/plugins/createWithPortableTextSelections.ts +65 -0
- package/src/editor/plugins/createWithSchemaTypes.ts +76 -0
- package/src/editor/plugins/createWithUndoRedo.ts +494 -0
- package/src/editor/plugins/createWithUtils.ts +81 -0
- package/src/editor/plugins/index.ts +155 -0
- package/src/index.ts +11 -0
- package/src/patch/PatchEvent.ts +33 -0
- package/src/patch/applyPatch.ts +29 -0
- package/src/patch/array.ts +89 -0
- package/src/patch/arrayInsert.ts +27 -0
- package/src/patch/object.ts +39 -0
- package/src/patch/patches.ts +53 -0
- package/src/patch/primitive.ts +43 -0
- package/src/patch/string.ts +51 -0
- package/src/types/editor.ts +576 -0
- package/src/types/options.ts +17 -0
- package/src/types/patch.ts +65 -0
- package/src/types/slate.ts +25 -0
- package/src/utils/__tests__/dmpToOperations.test.ts +181 -0
- package/src/utils/__tests__/operationToPatches.test.ts +421 -0
- package/src/utils/__tests__/patchToOperations.test.ts +293 -0
- package/src/utils/__tests__/ranges.test.ts +18 -0
- package/src/utils/__tests__/valueNormalization.test.tsx +62 -0
- package/src/utils/__tests__/values.test.ts +253 -0
- package/src/utils/applyPatch.ts +407 -0
- package/src/utils/bufferUntil.ts +15 -0
- package/src/utils/debug.ts +12 -0
- package/src/utils/getPortableTextMemberSchemaTypes.ts +100 -0
- package/src/utils/operationToPatches.ts +357 -0
- package/src/utils/patches.ts +36 -0
- package/src/utils/paths.ts +60 -0
- package/src/utils/ranges.ts +77 -0
- package/src/utils/schema.ts +8 -0
- package/src/utils/selection.ts +65 -0
- package/src/utils/ucs2Indices.ts +67 -0
- package/src/utils/validateValue.ts +394 -0
- package/src/utils/values.ts +208 -0
- package/src/utils/weakMaps.ts +24 -0
- package/src/utils/withChanges.ts +25 -0
- package/src/utils/withPreserveKeys.ts +14 -0
- package/src/utils/withoutPatching.ts +14 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
|
+
import {render, waitFor} from '@testing-library/react'
|
|
3
|
+
import {createRef, type RefObject} from 'react'
|
|
4
|
+
|
|
5
|
+
import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester'
|
|
6
|
+
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
|
+
|
|
8
|
+
const initialValue = [
|
|
9
|
+
{
|
|
10
|
+
_key: 'a',
|
|
11
|
+
_type: 'myTestBlockType',
|
|
12
|
+
children: [
|
|
13
|
+
{
|
|
14
|
+
_key: 'a1',
|
|
15
|
+
_type: 'span',
|
|
16
|
+
marks: [],
|
|
17
|
+
text: "It's a beautiful day on planet earth",
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
markDefs: [],
|
|
21
|
+
style: 'normal',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
_key: 'b',
|
|
25
|
+
_type: 'myTestBlockType',
|
|
26
|
+
children: [
|
|
27
|
+
{
|
|
28
|
+
_key: 'b1',
|
|
29
|
+
_type: 'span',
|
|
30
|
+
marks: [],
|
|
31
|
+
text: 'The birds are singing',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
markDefs: [],
|
|
35
|
+
style: 'normal',
|
|
36
|
+
},
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
describe('plugin:withPortableTextSelections', () => {
|
|
40
|
+
it('will report that a selection is made backward', async () => {
|
|
41
|
+
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
42
|
+
const onChange = jest.fn()
|
|
43
|
+
render(
|
|
44
|
+
<PortableTextEditorTester
|
|
45
|
+
onChange={onChange}
|
|
46
|
+
ref={editorRef}
|
|
47
|
+
schemaType={schemaType}
|
|
48
|
+
value={initialValue}
|
|
49
|
+
/>,
|
|
50
|
+
)
|
|
51
|
+
const initialSelection = {
|
|
52
|
+
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 9},
|
|
53
|
+
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 7},
|
|
54
|
+
}
|
|
55
|
+
await waitFor(() => {
|
|
56
|
+
if (editorRef.current) {
|
|
57
|
+
PortableTextEditor.focus(editorRef.current)
|
|
58
|
+
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
59
|
+
expect(PortableTextEditor.getSelection(editorRef.current)).toMatchInlineSnapshot(`
|
|
60
|
+
Object {
|
|
61
|
+
"anchor": Object {
|
|
62
|
+
"offset": 9,
|
|
63
|
+
"path": Array [
|
|
64
|
+
Object {
|
|
65
|
+
"_key": "b",
|
|
66
|
+
},
|
|
67
|
+
"children",
|
|
68
|
+
Object {
|
|
69
|
+
"_key": "b1",
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
"backward": true,
|
|
74
|
+
"focus": Object {
|
|
75
|
+
"offset": 7,
|
|
76
|
+
"path": Array [
|
|
77
|
+
Object {
|
|
78
|
+
"_key": "a",
|
|
79
|
+
},
|
|
80
|
+
"children",
|
|
81
|
+
Object {
|
|
82
|
+
"_key": "a1",
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
`)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
|
+
import {render, waitFor} from '@testing-library/react'
|
|
3
|
+
import {createRef, type RefObject} from 'react'
|
|
4
|
+
|
|
5
|
+
import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester'
|
|
6
|
+
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
|
+
|
|
8
|
+
const initialValue = [
|
|
9
|
+
{
|
|
10
|
+
_key: 'a',
|
|
11
|
+
_type: 'myTestBlockType',
|
|
12
|
+
children: [
|
|
13
|
+
{
|
|
14
|
+
_key: 'a1',
|
|
15
|
+
_type: 'span',
|
|
16
|
+
marks: [],
|
|
17
|
+
text: 'Block A',
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
markDefs: [],
|
|
21
|
+
style: 'normal',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
_key: 'b',
|
|
25
|
+
_type: 'myTestBlockType',
|
|
26
|
+
children: [
|
|
27
|
+
{
|
|
28
|
+
_key: 'b1',
|
|
29
|
+
_type: 'span',
|
|
30
|
+
marks: [],
|
|
31
|
+
text: 'Block B',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
markDefs: [],
|
|
35
|
+
style: 'normal',
|
|
36
|
+
},
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
const initialSelection = {
|
|
40
|
+
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
41
|
+
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('plugin:withUndoRedo', () => {
|
|
45
|
+
it('preserves the keys when undoing ', async () => {
|
|
46
|
+
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
47
|
+
const onChange = jest.fn()
|
|
48
|
+
render(
|
|
49
|
+
<PortableTextEditorTester
|
|
50
|
+
onChange={onChange}
|
|
51
|
+
ref={editorRef}
|
|
52
|
+
schemaType={schemaType}
|
|
53
|
+
value={initialValue}
|
|
54
|
+
/>,
|
|
55
|
+
)
|
|
56
|
+
await waitFor(() => {
|
|
57
|
+
if (editorRef.current) {
|
|
58
|
+
PortableTextEditor.focus(editorRef.current)
|
|
59
|
+
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
60
|
+
PortableTextEditor.delete(
|
|
61
|
+
editorRef.current,
|
|
62
|
+
PortableTextEditor.getSelection(editorRef.current),
|
|
63
|
+
{mode: 'blocks'},
|
|
64
|
+
)
|
|
65
|
+
expect(PortableTextEditor.getValue(editorRef.current)).toMatchInlineSnapshot(`
|
|
66
|
+
Array [
|
|
67
|
+
Object {
|
|
68
|
+
"_key": "a",
|
|
69
|
+
"_type": "myTestBlockType",
|
|
70
|
+
"children": Array [
|
|
71
|
+
Object {
|
|
72
|
+
"_key": "a1",
|
|
73
|
+
"_type": "span",
|
|
74
|
+
"marks": Array [],
|
|
75
|
+
"text": "Block A",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
"markDefs": Array [],
|
|
79
|
+
"style": "normal",
|
|
80
|
+
},
|
|
81
|
+
]
|
|
82
|
+
`)
|
|
83
|
+
PortableTextEditor.undo(editorRef.current)
|
|
84
|
+
expect(PortableTextEditor.getValue(editorRef.current)).toEqual(initialValue)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
it('preserves the keys when redoing ', async () => {
|
|
89
|
+
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
90
|
+
const onChange = jest.fn()
|
|
91
|
+
render(
|
|
92
|
+
<PortableTextEditorTester
|
|
93
|
+
onChange={onChange}
|
|
94
|
+
ref={editorRef}
|
|
95
|
+
schemaType={schemaType}
|
|
96
|
+
value={initialValue}
|
|
97
|
+
/>,
|
|
98
|
+
)
|
|
99
|
+
await waitFor(() => {
|
|
100
|
+
if (editorRef.current) {
|
|
101
|
+
PortableTextEditor.focus(editorRef.current)
|
|
102
|
+
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
103
|
+
PortableTextEditor.insertBlock(editorRef.current, editorRef.current.schemaTypes.block, {
|
|
104
|
+
children: [{_key: 'c1', _type: 'span', marks: [], text: 'Block C'}],
|
|
105
|
+
})
|
|
106
|
+
const producedKey = PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key
|
|
107
|
+
PortableTextEditor.undo(editorRef.current)
|
|
108
|
+
PortableTextEditor.redo(editorRef.current)
|
|
109
|
+
expect(PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key).toEqual(
|
|
110
|
+
producedKey,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
})
|