usfm-editor
Version:
A WYSIWYG editor component for USFM
124 lines (98 loc) • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MyTransforms = void 0;
require("core-js/modules/es.array.sort.js");
var _slate = require("slate");
var _NodeTypes = _interopRequireDefault(require("../../utils/NodeTypes"));
var _VerseTransforms = require("./VerseTransforms");
var _basicSlateNodeFactory = require("../../transforms/basicSlateNodeFactory");
var _UsfmMarkers = require("../../utils/UsfmMarkers");
var _SelectionTransforms = require("./SelectionTransforms");
var _identificationTransforms = require("../../transforms/identificationTransforms");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const MyTransforms = { ..._slate.Transforms,
..._VerseTransforms.VerseTransforms,
..._SelectionTransforms.SelectionTransforms,
mergeSelectedBlockAndSetToInlineContainer,
replaceNodes,
replaceText,
setIdentification
};
exports.MyTransforms = MyTransforms;
/**
* Merges the selected block with the next or previous block,
* then sets the resulting block to an inline container type.
*/
function mergeSelectedBlockAndSetToInlineContainer(editor, options) {
const {
mode = "previous"
} = options;
const selection = editor.selection;
if (!selection) return;
const [, selectedBlockPath] = _slate.Editor.parent(editor, selection.anchor.path);
const mergePath = mode === "previous" ? selectedBlockPath : _slate.Path.next(selectedBlockPath); // The path of the newly merged node
const resultingPath = mode === "previous" ? _slate.Path.previous(selectedBlockPath) : selectedBlockPath;
_slate.Editor.withoutNormalizing(editor, () => {
_slate.Transforms.mergeNodes(editor, {
at: mergePath
});
_slate.Transforms.setNodes(editor, {
type: _NodeTypes.default.INLINE_CONTAINER
}, {
at: resultingPath
});
});
}
/**
* Replaces the nodes at the given path with the desired nodes.
* Uses Editor.withoutNormalizing.
*
* @param editor Slate editor
* @param path Slate path of nodes to replace
* @param nodes The node or nodes that will replace the nodes at "path".
* The type of "nodes" matches the type required by Slate's
* Transforms.insertNodes.
*/
function replaceNodes(editor, path, nodes) {
_slate.Editor.withoutNormalizing(editor, () => {
_slate.Transforms.removeNodes(editor, {
at: path
});
_slate.Transforms.insertNodes(editor, nodes, {
at: path
});
});
}
function replaceText(editor, path, newText) {
_slate.Transforms.delete(editor, {
at: path
});
_slate.Transforms.insertNodes(editor, (0, _basicSlateNodeFactory.textNode)(newText), {
at: path
});
}
/**
* Sets the identification headers, stored in the "headers" node of
* the editor's children (at path [0].)
*
* @param {Editor} editor
* @param {Object} identification - Json specifying the identification headers
*/
function setIdentification(editor, identification) {
const sortedHeaders = (0, _identificationTransforms.identificationToSlate)(identification).sort((a, b) => _UsfmMarkers.UsfmMarkers.compare(a.type, b.type));
_slate.Editor.withoutNormalizing(editor, () => {
// Replace the existing identification headers
_slate.Transforms.removeNodes(editor, {
at: [0],
// look at headers only, not chapter contents
voids: true,
// captures nodes that aren't represented in the DOM
match: _UsfmMarkers.UsfmMarkers.isIdentification
});
_slate.Transforms.insertNodes(editor, sortedHeaders, {
at: [0, 0]
});
});
}