UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

66 lines 2.29 kB
import { EditorState, EditorView, baseKeymap, keymap, } from '../prosemirror'; import { default as defaultSchema } from './schema'; import { setTextSelection } from '../utils'; import { reactNodeViewPlugins } from '../plugins'; /** * Build a ProseMirror instance. * * Initial selection can be indicated using refs: * * - `<>` -- a collapsed text selection * - `<` and `>` -- a range text selection (`<` is from, `>` is to). */ export default function (options) { var plugins = []; var schema = options.schema || defaultSchema; var fixture = document.body.appendChild(document.createElement('div')); if (options.plugin) { plugins.push(options.plugin); } if (options.plugins) { plugins.push.apply(plugins, options.plugins); } plugins.push.apply(plugins, reactNodeViewPlugins(schema).concat([keymap(baseKeymap)])); var editorState = EditorState.create({ plugins: plugins, doc: options.doc, schema: schema, }); var editorView = new EditorView(fixture, { state: editorState, nodeViews: options.nodeViews || {}, }); var refs = editorState.doc.refs; // Collapsed selection. if ('<>' in refs) { setTextSelection(editorView, refs['<>']); // Expanded selection } else if ('<' in refs || '>' in refs) { if ('<' in refs === false) { throw new Error('A `<` ref must complement a `>` ref.'); } if ('>' in refs === false) { throw new Error('A `>` ref must complement a `<` ref.'); } setTextSelection(editorView, refs['<'], refs['>']); } var pluginStates = plugins.map(function (plugin) { return plugin.getState(editorState); }); afterEach(function () { editorView.destroy(); plugins.forEach(function (plugin) { return plugin.destroy && plugin.destroy(); }); if (fixture && fixture.parentNode === document.body) { document.body.removeChild(fixture); } }); return { editorView: editorView, plugins: plugins, pluginStates: pluginStates, refs: refs, plugin: plugins[0], pluginState: plugins[0].getState(editorState), sel: refs['<>'] }; }; //# sourceMappingURL=make-editor.js.map