@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
92 lines (90 loc) • 3.92 kB
JavaScript
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 const corePlugin = ({
config
}) => {
return {
name: 'core',
actions: {
execute: command => {
const editorView = config === null || config === void 0 ? void 0 : config.getEditorView();
if (!editorView || !command) {
return false;
}
const {
state,
dispatch
} = editorView;
return editorCommandToPMCommand(command)(state, dispatch);
},
// Code copied from `EditorActions.focus()`
focus: options => {
var _options$scrollIntoVi;
const 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: () => {
const 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: (replaceValue, options) => {
const editorView = config === null || config === void 0 ? void 0 : config.getEditorView();
if (!editorView || replaceValue === undefined || replaceValue === null) {
return false;
}
const {
state
} = editorView;
const {
schema
} = state;
const content = Array.isArray(replaceValue) ? processRawFragmentValue(schema, replaceValue) : processRawValue(schema, replaceValue);
if (content) {
var _options$scrollIntoVi2;
const 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(onReceive, options) {
var _config$getEditorView;
const 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(cb) {
var _config$getEditorView2;
const 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);
}
}
};
};