usfm-editor
Version:
A WYSIWYG editor component for USFM
168 lines (138 loc) • 6.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.filterInvalidIdentification = filterInvalidIdentification;
exports.identificationToSlate = identificationToSlate;
exports.mergeIdentification = mergeIdentification;
exports.normalizeIdentificationValues = normalizeIdentificationValues;
exports.parseIdentificationFromSlateTree = parseIdentificationFromSlateTree;
exports.parseIdentificationFromUsfm = parseIdentificationFromUsfm;
require("core-js/modules/es.array.flat-map.js");
require("core-js/modules/es.array.unscopables.flat-map.js");
var usfmjs = _interopRequireWildcard(require("usfm-js"));
var _slate = require("slate");
var _UsfmMarkers = require("../utils/UsfmMarkers");
var _usfmToSlate = require("./usfmToSlate");
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* Applies the desired updates to an identification json object
*
* @param {Object} current - Json specifying the current identificaton
* headers
* @param {Object} updates - Json specifying the identification headers
* to add, update, or remove, and the desired values.
* example: {'toc1': 'new_toc1', 'id': 'updated_id'}
* Unspecified headers will be kept the same.
* To delete a header, pass a null value like {'toc1': null}
* @returns The updated identification json
*/
function mergeIdentification(current, updates) {
const updatedJson = (0, _cloneDeep.default)(current);
Object.assign(updatedJson, updates);
Object.entries(updatedJson).forEach(([marker, value]) => {
if (value === null) {
delete updatedJson[marker];
}
});
return updatedJson;
}
function filterInvalidIdentification(ids) {
const validIdJson = {};
Object.entries(ids).forEach(([marker, value]) => {
if (!isValidIdentificationMarker(marker)) {
console.error("Invalid marker: ", marker);
} else if (!isValidMarkerValuePair(marker, value)) {
console.error("Invalid marker, value pair: ", marker, value);
} else {
validIdJson[marker] = value;
}
});
return validIdJson;
}
/**
* Normalizes all json values so that every non-null value is a string.
*/
function normalizeIdentificationValues(ids) {
const normalized = {};
Object.entries(ids).forEach(([marker, value]) => {
if (value === null) {
normalized[marker] = null;
} else if (Array.isArray(value)) {
normalized[marker] = value.map(v => v.toString());
} else {
normalized[marker] = value.toString();
}
});
return normalized;
}
function identificationToSlate(ids) {
function idHeader(tag, content) {
const nodes = asArray((0, _usfmToSlate.transformToSlate)({
tag,
content
}));
return nodes.flatMap(n => {
if (_slate.Element.isElement(n) || _slate.Text.isText(n)) {
return [n];
} else {
console.error("type error", n);
return [];
}
});
}
function asArray(x) {
return Array.isArray(x) ? x : [x];
}
const entries = Object.entries(ids).flatMap(([marker, vals]) => asArray(vals).filter(isNotNull).map(v => [marker, v]));
return entries.flatMap(([marker, value]) => idHeader(marker, value));
}
function parseIdentificationFromUsfm(usfm) {
const usfmJsDoc = usfmjs.toJSON(usfm);
const headersArray = usfmJsDoc.headers.filter(isIdHeader);
return arrayToIds(headersArray);
}
function parseIdentificationFromSlateTree(editor) {
const slateHeaders = editor.children[0] && _slate.Element.isElement(editor.children[0]) ? editor.children[0].children : [];
const headersArray = slateHeaders.map(node => ({
tag: node === null || node === void 0 ? void 0 : node.type,
content: _slate.Node.string(node)
}));
return arrayToIds(headersArray);
}
const isValidIdentificationMarker = marker => _UsfmMarkers.UsfmMarkers.isIdentification(marker) && _UsfmMarkers.UsfmMarkers.isValid(marker);
const isNumberOrString = value => typeof value === "string" || typeof value === "number";
function isValidMarkerValuePair(marker, value) {
if (value === null) return true;
const baseMarker = _UsfmMarkers.UsfmMarkers.getBaseMarker(marker);
if (baseMarker === _UsfmMarkers.UsfmMarkers.IDENTIFICATION.rem) {
return Array.isArray(value) && value.length > 0 && value.every(v => isNumberOrString(v));
} else {
return isNumberOrString(value);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isIdHeader(r) {
return typeof r.tag === "string" && typeof r.content === "string";
}
function arrayToIds(headersArray) {
const parsed = {};
let remarks = [];
headersArray.forEach(h => {
if (h.tag == _UsfmMarkers.UsfmMarkers.IDENTIFICATION.rem) {
remarks = remarks.concat(h.content);
} else if (_UsfmMarkers.UsfmMarkers.isIdentification(h.tag)) {
parsed[h.tag] = h.content;
}
});
if (remarks.length > 0) {
parsed[_UsfmMarkers.UsfmMarkers.IDENTIFICATION.rem] = remarks;
}
return parsed;
}
function isNotNull(t) {
return t !== null;
}