@atlaskit/editor-plugin-code-block
Version:
Code block plugin for @atlaskit/editor-core
74 lines (70 loc) • 2.87 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { insertBlock } from '@atlaskit/editor-common/commands';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { createRule, inputRuleWithAnalytics } from '@atlaskit/editor-common/utils';
import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
import { createPlugin, leafNodeReplacementCharacter } from '@atlaskit/prosemirror-input-rules';
import { isConvertableToCodeBlock, transformToCodeBlockAction } from './transform-to-code-block';
export function createCodeBlockInputRule(schema, editorAnalyticsAPI) {
var rules = getCodeBlockRules(editorAnalyticsAPI, schema);
return new SafePlugin(createPlugin('code-block-input-rule', rules, {
isBlockNodeRule: true
}));
}
/**
* Get all code block input rules
*
* @param {Schema} schema
* @returns {InputRuleWithHandler[]}
*/
function getCodeBlockRules(editorAnalyticsAPI, schema) {
var ruleAnalytics = inputRuleWithAnalytics({
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.DOCUMENT,
actionSubjectId: ACTION_SUBJECT_ID.CODE_BLOCK,
attributes: {
inputMethod: INPUT_METHOD.FORMATTING
},
eventType: EVENT_TYPE.TRACK
}, editorAnalyticsAPI);
var validMatchLength = function validMatchLength(match) {
return match.length > 0 && match[0].length === 3;
};
// eslint-disable-next-line require-unicode-regexp
var threeTildeRule = createRule(/(?!\s)(`{3,})$/, function (state, match, start, end) {
if (!validMatchLength(match)) {
return null;
}
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var attributes = {};
if (match[4]) {
attributes.language = match[4];
}
if (isConvertableToCodeBlock(state)) {
return transformToCodeBlockAction(state, start, attributes);
}
var tr = state.tr;
tr.delete(start, end);
var codeBlock = tr.doc.type.schema.nodes.codeBlock.createChecked();
safeInsert(codeBlock)(tr);
return tr;
});
var leftNodeReplacementThreeTildeRule = createRule(
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
new RegExp("((".concat(leafNodeReplacementCharacter, "`{3,})|^\\s(`{3,}))(\\S*)$")), function (state, match, start, end) {
if (!validMatchLength(match)) {
return null;
}
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var attributes = {};
if (match[4]) {
attributes.language = match[4];
}
var inlineStart = Math.max(match.index + state.selection.$from.start(), 1);
return insertBlock(state, schema.nodes.codeBlock, inlineStart, end, attributes);
});
return [ruleAnalytics(threeTildeRule), ruleAnalytics(leftNodeReplacementThreeTildeRule)];
}