UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

85 lines (83 loc) 4.02 kB
import { processRawFragmentValue, processRawValue } from '../../utils/processRawValue'; import { editorCommandToPMCommand } from '../editor-commands'; import { scheduleDocumentRequest } from './requestDocument'; /** * Core plugin that is always included in the preset. * Allows for executing `EditorCommand` and other core functionality. */ export var corePlugin = function corePlugin(_ref) { var config = _ref.config; return { name: 'core', actions: { execute: function execute(command) { var editorView = config === null || config === void 0 ? void 0 : config.getEditorView(); if (!editorView || !command) { return false; } var state = editorView.state, dispatch = editorView.dispatch; return editorCommandToPMCommand(command)(state, dispatch); }, // Code copied from `EditorActions.focus()` focus: function focus(options) { var _options$scrollIntoVi; var editorView = config === null || config === void 0 ? void 0 : config.getEditorView(); if (!editorView || editorView.hasFocus()) { return false; } editorView.focus(); if ((_options$scrollIntoVi = options === null || options === void 0 ? void 0 : options.scrollIntoView) !== null && _options$scrollIntoVi !== void 0 ? _options$scrollIntoVi : true) { editorView.dispatch(editorView.state.tr.scrollIntoView()); } return true; }, // Code copied from `EditorActions.blur()` blur: function blur() { var editorView = config === null || config === void 0 ? void 0 : config.getEditorView(); if (!editorView || !editorView.hasFocus()) { return false; } // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting editorView.dom.blur(); return true; }, replaceDocument: function replaceDocument(replaceValue, options) { var editorView = config === null || config === void 0 ? void 0 : config.getEditorView(); if (!editorView || replaceValue === undefined || replaceValue === null) { return false; } var state = editorView.state; var schema = state.schema; var content = Array.isArray(replaceValue) ? processRawFragmentValue(schema, replaceValue) : processRawValue(schema, replaceValue); if (content) { var _options$scrollIntoVi2; var tr = state.tr.replaceWith(0, state.doc.nodeSize - 2, content); if ((_options$scrollIntoVi2 = options === null || options === void 0 ? void 0 : options.scrollIntoView) !== null && _options$scrollIntoVi2 !== void 0 ? _options$scrollIntoVi2 : true) { editorView.dispatch(tr.scrollIntoView()); } else { editorView.dispatch(tr); } return true; } return false; }, // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any requestDocument: function requestDocument(onReceive, options) { var _config$getEditorView; var view = (_config$getEditorView = config === null || config === void 0 ? void 0 : config.getEditorView()) !== null && _config$getEditorView !== void 0 ? _config$getEditorView : null; scheduleDocumentRequest(view, onReceive, options === null || options === void 0 ? void 0 : options.transformer, config === null || config === void 0 ? void 0 : config.fireAnalyticsEvent); }, createTransformer: function createTransformer(cb) { var _config$getEditorView2; var view = (_config$getEditorView2 = config === null || config === void 0 ? void 0 : config.getEditorView()) !== null && _config$getEditorView2 !== void 0 ? _config$getEditorView2 : null; if (!(view !== null && view !== void 0 && view.state.schema)) { return undefined; } return cb(view === null || view === void 0 ? void 0 : view.state.schema); } } }; };