mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-19 21:51:51 +02:00
Add editor.HasCommand
and editor.HasQuery
(#2438)
* Add `editor.hasCommand` method * Add `editor.hasQuery` method * Rename directories `hasCommand` and `hasQuery` to kebab case * Add tests for `editor.hasCommand` of React component * Add tests for `editor.hasQuery` of React component
This commit is contained in:
committed by
Ian Storm Taylor
parent
5cca509db2
commit
76a88a649a
@@ -39,6 +39,7 @@
|
||||
"devDependencies": {
|
||||
"immutable": "^3.8.1",
|
||||
"mocha": "^2.5.3",
|
||||
"react-test-renderer": "^16.6.3",
|
||||
"slate": "^0.44.8",
|
||||
"slate-hyperscript": "^0.11.23",
|
||||
"slate-simulator": "^0.4.67"
|
||||
|
@@ -243,6 +243,14 @@ class Editor extends React.Component {
|
||||
return this.controller.command(...args)
|
||||
}
|
||||
|
||||
hasCommand(...args) {
|
||||
return this.controller.hasCommand(...args)
|
||||
}
|
||||
|
||||
hasQuery(...args) {
|
||||
return this.controller.hasQuery(...args)
|
||||
}
|
||||
|
||||
normalize(...args) {
|
||||
return this.controller.normalize(...args)
|
||||
}
|
||||
|
@@ -0,0 +1,13 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
export const input = { defaultValue }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('insertText')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,21 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
commands: {
|
||||
customCommand: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = { defaultValue, plugins }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('customCommand')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,21 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
commands: {
|
||||
customCommand: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = { defaultValue, plugins }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('otherCommand')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,13 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
export const input = { defaultValue }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('isVoid')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,21 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
queries: {
|
||||
customQuery: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = { defaultValue, plugins }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('customQuery')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,21 @@
|
||||
/** @jsx h */
|
||||
|
||||
import Plain from 'slate-plain-serializer'
|
||||
|
||||
const defaultValue = Plain.deserialize('')
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
queries: {
|
||||
customQuery: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = { defaultValue, plugins }
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('otherQuery')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -2,11 +2,25 @@ import assert from 'assert'
|
||||
import clean from './helpers/clean'
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/server'
|
||||
import ShallowRenderer from 'react-test-renderer/shallow'
|
||||
import { Editor } from 'slate-react'
|
||||
import { fixtures } from 'slate-dev-test-utils'
|
||||
import { JSDOM } from 'jsdom'
|
||||
|
||||
describe('slate-react', () => {
|
||||
fixtures(__dirname, 'components', ({ module }) => {
|
||||
const { input, output, default: fn } = module
|
||||
|
||||
const renderer = new ShallowRenderer()
|
||||
renderer.render(React.createElement(Editor, input, null))
|
||||
const editor = renderer.getRenderOutput().props.editor
|
||||
|
||||
const actual = fn(editor)
|
||||
const expected = output
|
||||
|
||||
assert.equal(actual, expected)
|
||||
})
|
||||
|
||||
fixtures(__dirname, 'rendering/fixtures', ({ module }) => {
|
||||
const { value, output, props } = module
|
||||
const p = {
|
||||
|
@@ -158,6 +158,34 @@ class Editor {
|
||||
return controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a command by `type` has been registered.
|
||||
*
|
||||
* @param {String} type
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
hasCommand(type) {
|
||||
const { controller } = this
|
||||
const has = type in controller && controller[type].__command
|
||||
|
||||
return has
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a query by `type` has been registered.
|
||||
*
|
||||
* @param {String} type
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
hasQuery(type) {
|
||||
const { controller } = this
|
||||
const has = type in controller && controller[type].__query
|
||||
|
||||
return has
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize all of the nodes in the document from scratch.
|
||||
*
|
||||
|
@@ -0,0 +1,19 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
commands: {
|
||||
customCommand: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = new Editor({ plugins })
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('customCommand')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,11 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
export const input = new Editor().registerCommand('customCommand')
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('customCommand')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,19 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
commands: {
|
||||
customCommand: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = new Editor({ plugins })
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('otherCommand')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,11 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
export const input = new Editor().registerCommand('customCommand')
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasCommand('otherCommand')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,19 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
queries: {
|
||||
customQuery: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = new Editor({ plugins })
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('customQuery')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,11 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
export const input = new Editor().registerQuery('customQuery')
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('customQuery')
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,19 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
const plugins = [
|
||||
{
|
||||
queries: {
|
||||
customquery: () => {},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const input = new Editor({ plugins })
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('otherquery')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,11 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { Editor } from 'slate'
|
||||
|
||||
export const input = new Editor().registerQuery('customQuery')
|
||||
|
||||
export default function(editor) {
|
||||
return editor.hasQuery('otherQuery')
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -126,6 +126,15 @@ describe('slate', () => {
|
||||
assert.deepEqual(actual, expected)
|
||||
})
|
||||
|
||||
fixtures(__dirname, 'controllers', ({ module }) => {
|
||||
const { input, output, default: fn } = module
|
||||
|
||||
const actual = fn(input)
|
||||
const expected = output
|
||||
|
||||
assert.equal(actual, expected)
|
||||
})
|
||||
|
||||
fixtures(__dirname, 'schema', ({ module }) => {
|
||||
const { input, output, schema } = module
|
||||
const editor = new Editor({ value: input, plugins: [{ schema }] })
|
||||
|
Reference in New Issue
Block a user