@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
90 lines (88 loc) • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.corePlugin = void 0;
var _processRawValue = require("../../utils/processRawValue");
var _editorCommands = require("../editor-commands");
var _requestDocument = require("./requestDocument");
/**
* Core plugin that is always included in the preset.
* Allows for executing `EditorCommand` and other core functionality.
*/
var corePlugin = exports.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 (0, _editorCommands.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) ? (0, _processRawValue.processRawFragmentValue)(schema, replaceValue) : (0, _processRawValue.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;
(0, _requestDocument.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);
}
}
};
};