Skip to content

Commit c29eea0

Browse files
authored
Allow passing custom editor creator to slate-hyperscript createEditor (ianstormtaylor#4555)
* Allow passing custom slate editor creator to slate-hyperscript createEditor() Makes it easier to create your own testing setup * run fix:prettier * remove unused createEditor * Add changeset * fix lint and remove accidentally committed file
1 parent b18dd40 commit c29eea0

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

.changeset/fast-shrimps-begin.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'slate-hyperscript': patch
3+
---
4+
5+
createEditor is now exported from slate-hyperscript, making it easier to set up custom editor tests
6+
7+
For example:
8+
9+
```
10+
const jsx = createHyperscript({
11+
creators: {
12+
editor: createEditor(aFunctionThatReturnsAnEditorObject)
13+
},
14+
elements: {
15+
block: { type: 'block' },
16+
inline: { type: 'inline' }
17+
}
18+
})
19+
```

packages/slate-hyperscript/src/creators.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
Element,
3-
Descendant,
4-
Node,
5-
Range,
6-
Text,
7-
Editor,
8-
createEditor as makeEditor,
9-
} from 'slate'
1+
import { Element, Descendant, Node, Range, Text, Editor } from 'slate'
102
import {
113
AnchorToken,
124
FocusToken,
@@ -217,11 +209,11 @@ export function createText(
217209
* Create a top-level `Editor` object.
218210
*/
219211

220-
export function createEditor(
212+
export const createEditor = (makeEditor: () => Editor) => (
221213
tagName: string,
222214
attributes: { [key: string]: any },
223215
children: any[]
224-
): Editor {
216+
): Editor => {
225217
const otherChildren: any[] = []
226218
let selectionChild: Range | undefined
227219

packages/slate-hyperscript/src/hyperscript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isPlainObject } from 'is-plain-object'
2-
import { Element } from 'slate'
2+
import { Element, createEditor as makeEditor } from 'slate'
33
import {
44
createAnchor,
55
createCursor,
@@ -18,7 +18,7 @@ import {
1818
const DEFAULT_CREATORS = {
1919
anchor: createAnchor,
2020
cursor: createCursor,
21-
editor: createEditor,
21+
editor: createEditor(makeEditor),
2222
element: createElement,
2323
focus: createFocus,
2424
fragment: createFragment,

packages/slate-hyperscript/src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ import {
33
HyperscriptCreators,
44
HyperscriptShorthands,
55
} from './hyperscript'
6+
import { createEditor } from './creators'
67

78
/**
89
* The default hyperscript factory that ships with Slate, without custom tags.
910
*/
1011

1112
const jsx = createHyperscript()
1213

13-
export { jsx, createHyperscript, HyperscriptCreators, HyperscriptShorthands }
14+
export {
15+
jsx,
16+
createHyperscript,
17+
createEditor,
18+
HyperscriptCreators,
19+
HyperscriptShorthands,
20+
}

0 commit comments

Comments
 (0)