@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
113 lines • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../prosemirror");
var groups_1 = require("./groups");
var marks_1 = require("./marks");
var nodes_1 = require("./nodes");
function addItems(builtInItems, config, customSpecs) {
if (customSpecs === void 0) { customSpecs = {}; }
if (!config) {
return {};
}
/**
* Add built-in Node / Mark specs
*/
var items = builtInItems.reduce(function (items, _a) {
var name = _a.name, spec = _a.spec;
if (config.indexOf(name) !== -1) {
items[name] = customSpecs[name] || spec;
}
return items;
}, {});
/**
* Add Custom Node / Mark specs
*/
return Object.keys(customSpecs).reduce(function (items, name) {
if (items[name]) {
return items;
}
items[name] = customSpecs[name];
return items;
}, items);
}
// We use groups to allow schemas to be constructed in different shapes without changing node/mark
// specs, but this means nodes/marks are defined with groups that might never be used in the schema.
// In this scenario ProseMirror will complain and prevent the schema from being constructed.
//
// To avoid the problem, we include items that serve to "declare" the groups in the schema. This
// approach unfortunately leaves unused items in the schema, but has the benefit of avoiding the
// need to manipulate `exclude` or content expression values for potentially every schema item.
function groupDeclaration(name) {
return {
name: "__" + name + "GroupDeclaration",
spec: {
group: name,
}
};
}
var markGroupDeclarations = [
groupDeclaration(groups_1.COLOR),
groupDeclaration(groups_1.FONT_STYLE),
groupDeclaration(groups_1.SEARCH_QUERY),
groupDeclaration(groups_1.LINK),
];
var markGroupDeclarationsNames = markGroupDeclarations.map(function (groupMark) { return groupMark.name; });
var nodesInOrder = [
{ name: 'doc', spec: nodes_1.doc },
{ name: 'paragraph', spec: nodes_1.paragraph },
{ name: 'text', spec: nodes_1.text },
{ name: 'bulletList', spec: nodes_1.bulletList },
{ name: 'orderedList', spec: nodes_1.orderedList },
{ name: 'listItem', spec: nodes_1.listItem },
{ name: 'heading', spec: nodes_1.heading },
{ name: 'blockquote', spec: nodes_1.blockquote },
{ name: 'codeBlock', spec: nodes_1.codeBlock },
{ name: 'panel', spec: nodes_1.panel },
{ name: 'rule', spec: nodes_1.rule },
{ name: 'image', spec: nodes_1.image },
{ name: 'mention', spec: nodes_1.mention },
{ name: 'media', spec: nodes_1.media },
{ name: 'mediaGroup', spec: nodes_1.mediaGroup },
{ name: 'singleImage', spec: nodes_1.singleImage },
{ name: 'hardBreak', spec: nodes_1.hardBreak },
{ name: 'emoji', spec: nodes_1.emoji },
{ name: 'table', spec: nodes_1.table },
{ name: 'tableCell', spec: nodes_1.tableCell },
{ name: 'tableRow', spec: nodes_1.tableRow },
{ name: 'tableHeader', spec: nodes_1.tableHeader },
{ name: 'confluenceJiraIssue', spec: nodes_1.confluenceJiraIssue },
{ name: 'confluenceUnsupportedInline', spec: nodes_1.confluenceUnsupportedInline },
{ name: 'confluenceUnsupportedBlock', spec: nodes_1.confluenceUnsupportedBlock },
{ name: 'applicationCard', spec: nodes_1.applicationCard },
{ name: 'decisionList', spec: nodes_1.decisionList },
{ name: 'decisionItem', spec: nodes_1.decisionItem },
{ name: 'taskList', spec: nodes_1.taskList },
{ name: 'taskItem', spec: nodes_1.taskItem },
{ name: 'unknownBlock', spec: nodes_1.unknownBlock },
];
var marksInOrder = [
{ name: 'link', spec: marks_1.link },
{ name: 'em', spec: marks_1.em },
{ name: 'strong', spec: marks_1.strong },
{ name: 'strike', spec: marks_1.strike },
{ name: 'subsup', spec: marks_1.subsup },
{ name: 'underline', spec: marks_1.underline },
{ name: 'code', spec: marks_1.code },
{ name: 'mentionQuery', spec: marks_1.mentionQuery },
{ name: 'emojiQuery', spec: marks_1.emojiQuery },
{ name: 'textColor', spec: marks_1.textColor }
].concat(markGroupDeclarations);
/**
* Creates a schema preserving order of marks and nodes.
*/
function createSchema(config) {
var nodes = config.nodes, customNodeSpecs = config.customNodeSpecs, marks = config.marks, customMarkSpecs = config.customMarkSpecs;
var nodesConfig = Object.keys(customNodeSpecs || {}).concat(nodes);
var marksConfig = Object.keys(customMarkSpecs || {}).concat(marks || []).concat(markGroupDeclarationsNames);
return new prosemirror_1.Schema({
nodes: addItems(nodesInOrder, nodesConfig, customNodeSpecs),
marks: addItems(marksInOrder, marksConfig, customMarkSpecs),
});
}
exports.createSchema = createSchema;
//# sourceMappingURL=create-schema.js.map