@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
68 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../prosemirror");
var schema_1 = require("./schema");
var utils_1 = require("../utils");
var plugins_1 = require("../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).
*/
exports.default = function (options) {
var plugins = [];
var schema = options.schema || schema_1.default;
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, plugins_1.reactNodeViewPlugins(schema).concat([prosemirror_1.keymap(prosemirror_1.baseKeymap)]));
var editorState = prosemirror_1.EditorState.create({
plugins: plugins,
doc: options.doc,
schema: schema,
});
var editorView = new prosemirror_1.EditorView(fixture, {
state: editorState,
nodeViews: options.nodeViews || {},
});
var refs = editorState.doc.refs;
// Collapsed selection.
if ('<>' in refs) {
utils_1.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.');
}
utils_1.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