Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing custom editor creator to slate-hyperscript createEditor #4555

Merged
merged 5 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/fast-shrimps-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'slate-hyperscript': patch
---

createEditor is now exported from slate-hyperscript, making it easier to set up custom editor tests

For example:

```
const jsx = createHyperscript({
creators: {
editor: createEditor(aFunctionThatReturnsAnEditorObject)
},
elements: {
block: { type: 'block' },
inline: { type: 'inline' }
}
})
```
14 changes: 3 additions & 11 deletions packages/slate-hyperscript/src/creators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Element,
Descendant,
Node,
Range,
Text,
Editor,
createEditor as makeEditor,
} from 'slate'
import { Element, Descendant, Node, Range, Text, Editor } from 'slate'
import {
AnchorToken,
FocusToken,
Expand Down Expand Up @@ -217,11 +209,11 @@ export function createText(
* Create a top-level `Editor` object.
*/

export function createEditor(
export const createEditor = (makeEditor: () => Editor) => (
tagName: string,
attributes: { [key: string]: any },
children: any[]
): Editor {
): Editor => {
const otherChildren: any[] = []
let selectionChild: Range | undefined

Expand Down
4 changes: 2 additions & 2 deletions packages/slate-hyperscript/src/hyperscript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPlainObject } from 'is-plain-object'
import { Element } from 'slate'
import { Element, createEditor as makeEditor } from 'slate'
import {
createAnchor,
createCursor,
Expand All @@ -18,7 +18,7 @@ import {
const DEFAULT_CREATORS = {
anchor: createAnchor,
cursor: createCursor,
editor: createEditor,
editor: createEditor(makeEditor),
element: createElement,
focus: createFocus,
fragment: createFragment,
Expand Down
9 changes: 8 additions & 1 deletion packages/slate-hyperscript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import {
HyperscriptCreators,
HyperscriptShorthands,
} from './hyperscript'
import { createEditor } from './creators'

/**
* The default hyperscript factory that ships with Slate, without custom tags.
*/

const jsx = createHyperscript()

export { jsx, createHyperscript, HyperscriptCreators, HyperscriptShorthands }
export {
jsx,
createHyperscript,
createEditor,
HyperscriptCreators,
HyperscriptShorthands,
}