UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

67 lines 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var prosemirror_1 = require("../../prosemirror"); var analytics_1 = require("../../analytics"); var transform_to_code_1 = require("./transform-to-code"); var utils_1 = require("../utils"); function addMark(markType, schema, charSize) { return function (state, match, start, end) { var to = end; // in case of *string* pattern it matches the text from beginning of the paragraph, // because we want ** to work for strong text // that's why "start" argument is wrong and we need to calculate it ourselves var from = match[1] ? to - match[1].length + 1 : start; // fixes the following case: my `*name` is * // expected result: should ignore special characters inside "code" if (state.schema.marks.code && state.schema.marks.code.isInSet(state.doc.resolve(from + 1).marks())) { return; } analytics_1.analyticsService.trackEvent("atlassian.editor.format." + markType.name + ".autoformatting"); // apply mark to the range (from, to) var tr = state.tr.addMark(from, to, markType.create()); if (charSize > 1) { // delete special characters after the text // Prosemirror removes the last symbol by itself, so we need to remove "charSize - 1" symbols tr = tr.delete(to - (charSize - 1), to); } return tr .delete(from, from + charSize) .removeStoredMark(markType); }; } function addCodeMark(markType, schema, specialChar) { return function (state, match, start, end) { analytics_1.analyticsService.trackEvent('atlassian.editor.format.code.autoformatting'); return transform_to_code_1.transformToCodeAction(state, start, end).delete(start, start + specialChar.length).removeStoredMark(markType); }; } function inputRulePlugin(schema) { var rules = []; if (schema.marks.strong) { // **string** or __strong__ should bold the text var markLength = 2; rules.push(utils_1.createInputRule(/(?:[^`0-9A-Za-z]+)(\_\_([^\s\_][^\_]+)\_\_)$|^(\_\_([^\s \_][^\_]+)\_\_)$/, addMark(schema.marks.strong, schema, markLength))); rules.push(utils_1.createInputRule(/^(?:[^`]+)(\*\*([^\s\*][^\*]+)\*\*)$|^(\*\*([^\s\*][^\*]+)\*\*)$/, addMark(schema.marks.strong, schema, markLength))); } if (schema.marks.em) { // *string* or _string_ should italic the text var markLength = 1; rules.push(utils_1.createInputRule(/(?:[^\_`0-9A-Za-z]+)(\_([^\s\_][^\_]+?)\_)$|^(\_([^\s\_][^\_]+)\_)$/, addMark(schema.marks.em, schema, markLength))); rules.push(utils_1.createInputRule(/^(?:[^\*`]+)(\*([^\s\*][^\*]+?)\*)$|^(\*([^\s\*][^\*]+)\*)$/, addMark(schema.marks.em, schema, markLength))); } if (schema.marks.strike) { // ~~string~~ should strikethrough the text var markLength = 2; rules.push(utils_1.createInputRule(/^(?:[^`]+)(\~\~([^\s\~][^\~]+)\~\~)$|^(\~\~([^\s\~][^\~]+)\~\~)$/, addMark(schema.marks.strike, schema, markLength))); } if (schema.marks.code) { // `string` should monospace the text rules.push(utils_1.createInputRule(/(`([^\s`][^`]+)`)$/, addCodeMark(schema.marks.code, schema, '`'))); } if (rules.length !== 0) { return prosemirror_1.inputRules({ rules: rules }); } } exports.inputRulePlugin = inputRulePlugin; exports.default = inputRulePlugin; //# sourceMappingURL=input-rule.js.map