@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
185 lines • 7.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var prosemirror_1 = require("../../prosemirror");
var MarkdownIt = require("markdown-it");
var markdown_it_table_1 = require("markdown-it-table");
var table_1 = require("../table");
var utils_1 = require("../table/utils");
var util_1 = require("./util");
function isCode(str) {
var lines = str.split(/\r?\n|\r/);
if (3 > lines.length) {
return false;
}
var weight = 0;
lines.forEach(function (line) {
// Ends with : or ;
if (/[:;]$/.test(line)) {
weight++;
}
// Contains second and third braces
if (/[{}\[\]]/.test(line)) {
weight++;
}
// Contains <tag> or </
if ((/<\w+>/.test(line) || /<\//.test(line))) {
weight++;
}
// Contains () <- function calls
if (/\(\)/.test(line)) {
weight++;
}
// New line starts with less than two chars. e.g- if, {, <, etc
var token = /^(\s+)[a-zA-Z<{]{2,}/.exec(line);
if (token && 2 <= token[1].length) {
weight++;
}
if (/&&/.test(line)) {
weight++;
}
});
return 4 <= weight && weight >= 0.5 * lines.length;
}
exports.stateKey = new prosemirror_1.PluginKey('pastePlugin');
var pmSchemaToMdMapping = {
nodes: {
blockquote: 'blockquote',
paragraph: 'paragraph',
rule: 'hr',
// lheading (---, ===)
heading: ['heading', 'lheading'],
codeBlock: ['code', 'fence'],
listItem: 'list',
image: 'image',
},
marks: {
em: 'emphasis',
strong: 'text',
link: ['link', 'autolink', 'reference', 'linkify'],
strike: 'strikethrough',
code: 'backticks',
}
};
exports.createPlugin = function (schema) {
var atlassianMarkDownParser;
var md = MarkdownIt('zero', { html: false, linkify: true });
md.enable([
// Process html entity - {, ¯, ", ...
'entity',
// Process escaped chars and hardbreaks
'escape'
]);
// Enable markdown plugins based on schema
['nodes', 'marks'].forEach(function (key) {
for (var idx in pmSchemaToMdMapping[key]) {
if (schema[key][idx]) {
md.enable(pmSchemaToMdMapping[key][idx]);
}
}
});
if (schema.nodes.table) {
md.use(markdown_it_table_1.default);
}
var filterMdToPmSchemaMapping = function (map) { return Object.keys(map).reduce(function (newMap, key) {
var value = map[key];
var block = value.block || value.node;
var mark = value.mark;
if ((block && schema.nodes[block]) || (mark && schema.marks[mark])) {
newMap[key] = value;
}
return newMap;
}, {}); };
atlassianMarkDownParser = new prosemirror_1.MarkdownParser(schema, md, filterMdToPmSchemaMapping({
blockquote: { block: 'blockquote' },
paragraph: { block: 'paragraph' },
em: { mark: 'em' },
strong: { mark: 'strong' },
link: {
mark: 'link', attrs: function (tok) { return ({
href: tok.attrGet('href'),
title: tok.attrGet('title') || null
}); }
},
hr: { node: 'rule' },
heading: { block: 'heading', attrs: function (tok) { return ({ level: +tok.tag.slice(1) }); } },
code_block: { block: 'codeBlock' },
list_item: { block: 'listItem' },
bullet_list: { block: 'bulletList' },
ordered_list: { block: 'orderedList', attrs: function (tok) { return ({ order: +tok.attrGet('order') || 1 }); } },
code_inline: { mark: 'code' },
fence: { block: 'codeBlock', attrs: function (tok) { return ({ language: tok.info || '' }); } },
image: {
node: 'image', attrs: function (tok) { return ({
src: tok.attrGet('src'),
title: tok.attrGet('title') || null,
alt: tok.children[0] && tok.children[0].content || null,
}); }
},
emoji: {
node: 'emoji', attrs: function (tok) { return ({
shortName: ":" + tok.markup + ":",
text: tok.content,
}); }
},
table: { block: 'table' },
tr: { block: 'tableRow' },
th: { block: 'tableHeader' },
td: { block: 'tableCell' },
s: { mark: 'strike' },
}));
return new prosemirror_1.Plugin({
key: exports.stateKey,
props: {
handlePaste: function (view, event, slice) {
if (!event.clipboardData) {
return false;
}
var text = event.clipboardData.getData('text/plain');
var html = event.clipboardData.getData('text/html');
var node = slice.content.firstChild;
var schema = view.state.schema;
var $from = view.state.selection.$from;
var selectedNode = $from.node($from.depth);
if (text && selectedNode.type === schema.nodes.codeBlock) {
view.dispatch(view.state.tr.insertText(text));
return true;
}
if ((text && isCode(text)) ||
(text && html && node && node.type === schema.nodes.codeBlock)) {
var tr = void 0;
if (util_1.isSingleLine(text)) {
tr = view.state.tr.insertText(text);
tr = tr.addMark($from.pos, $from.pos + text.length, schema.marks.code.create());
}
else {
var codeBlockNode = schema.nodes.codeBlock.create(node ? node.attrs : {}, schema.text(text));
tr = view.state.tr.replaceSelectionWith(codeBlockNode);
}
view.dispatch(tr.scrollIntoView());
return true;
}
if (text && !html && atlassianMarkDownParser) {
var doc = atlassianMarkDownParser.parse(text);
if (doc && doc.content) {
var tr = view.state.tr.replaceSelection(new prosemirror_1.Slice(doc.content, slice.openStart, slice.openEnd));
view.dispatch(tr.scrollIntoView());
return true;
}
}
if (html) {
var tableState = table_1.stateKey.getState(view.state);
if (tableState && tableState.isRequiredToAddHeader() && utils_1.containsTable(view, slice)) {
var state = view.state, dispatch = view.dispatch;
var selectionStart = state.selection.$from.pos;
dispatch(state.tr.replaceSelection(slice));
tableState.addHeaderToTableNodes(slice, selectionStart);
return true;
}
}
return false;
},
}
});
};
exports.default = function (schema) { return [exports.createPlugin(schema)]; };
//# sourceMappingURL=index.js.map