index.cjs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  5. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  6. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. var prosemirrorState = require('prosemirror-state');
  9. var prosemirrorTransform = require('prosemirror-transform');
  10. var InputRule = _createClass(function InputRule(match, handler) {
  11. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  12. _classCallCheck(this, InputRule);
  13. this.match = match;
  14. this.match = match;
  15. this.handler = typeof handler == "string" ? stringHandler(handler) : handler;
  16. this.undoable = options.undoable !== false;
  17. this.inCode = options.inCode || false;
  18. });
  19. function stringHandler(string) {
  20. return function (state, match, start, end) {
  21. var insert = string;
  22. if (match[1]) {
  23. var offset = match[0].lastIndexOf(match[1]);
  24. insert += match[0].slice(offset + match[1].length);
  25. start += offset;
  26. var cutOff = start - end;
  27. if (cutOff > 0) {
  28. insert = match[0].slice(offset - cutOff, offset) + insert;
  29. start = end;
  30. }
  31. }
  32. return state.tr.insertText(insert, start, end);
  33. };
  34. }
  35. var MAX_MATCH = 500;
  36. function inputRules(_ref) {
  37. var rules = _ref.rules;
  38. var plugin = new prosemirrorState.Plugin({
  39. state: {
  40. init: function init() {
  41. return null;
  42. },
  43. apply: function apply(tr, prev) {
  44. var stored = tr.getMeta(this);
  45. if (stored) return stored;
  46. return tr.selectionSet || tr.docChanged ? null : prev;
  47. }
  48. },
  49. props: {
  50. handleTextInput: function handleTextInput(view, from, to, text) {
  51. return run(view, from, to, text, rules, plugin);
  52. },
  53. handleDOMEvents: {
  54. compositionend: function compositionend(view) {
  55. setTimeout(function () {
  56. var $cursor = view.state.selection.$cursor;
  57. if ($cursor) run(view, $cursor.pos, $cursor.pos, "", rules, plugin);
  58. });
  59. }
  60. }
  61. },
  62. isInputRules: true
  63. });
  64. return plugin;
  65. }
  66. function run(view, from, to, text, rules, plugin) {
  67. if (view.composing) return false;
  68. var state = view.state,
  69. $from = state.doc.resolve(from);
  70. var textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - MAX_MATCH), $from.parentOffset, null, "\uFFFC") + text;
  71. for (var i = 0; i < rules.length; i++) {
  72. var rule = rules[i];
  73. if ($from.parent.type.spec.code) {
  74. if (!rule.inCode) continue;
  75. } else if (rule.inCode === "only") {
  76. continue;
  77. }
  78. var match = rule.match.exec(textBefore);
  79. var tr = match && rule.handler(state, match, from - (match[0].length - text.length), to);
  80. if (!tr) continue;
  81. if (rule.undoable) tr.setMeta(plugin, {
  82. transform: tr,
  83. from: from,
  84. to: to,
  85. text: text
  86. });
  87. view.dispatch(tr);
  88. return true;
  89. }
  90. return false;
  91. }
  92. var undoInputRule = function undoInputRule(state, dispatch) {
  93. var plugins = state.plugins;
  94. for (var i = 0; i < plugins.length; i++) {
  95. var plugin = plugins[i],
  96. undoable = void 0;
  97. if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
  98. if (dispatch) {
  99. var tr = state.tr,
  100. toUndo = undoable.transform;
  101. for (var j = toUndo.steps.length - 1; j >= 0; j--) tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
  102. if (undoable.text) {
  103. var marks = tr.doc.resolve(undoable.from).marks();
  104. tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks));
  105. } else {
  106. tr["delete"](undoable.from, undoable.to);
  107. }
  108. dispatch(tr);
  109. }
  110. return true;
  111. }
  112. }
  113. return false;
  114. };
  115. var emDash = new InputRule(/--$/, "—");
  116. var ellipsis = new InputRule(/\.\.\.$/, "…");
  117. var openDoubleQuote = new InputRule(/(?:^|[\s\{\[\(\<'"\u2018\u201C])(")$/, "“");
  118. var closeDoubleQuote = new InputRule(/"$/, "”");
  119. var openSingleQuote = new InputRule(/(?:^|[\s\{\[\(\<'"\u2018\u201C])(')$/, "‘");
  120. var closeSingleQuote = new InputRule(/'$/, "’");
  121. var smartQuotes = [openDoubleQuote, closeDoubleQuote, openSingleQuote, closeSingleQuote];
  122. function wrappingInputRule(regexp, nodeType) {
  123. var getAttrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  124. var joinPredicate = arguments.length > 3 ? arguments[3] : undefined;
  125. return new InputRule(regexp, function (state, match, start, end) {
  126. var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
  127. var tr = state.tr["delete"](start, end);
  128. var $start = tr.doc.resolve(start),
  129. range = $start.blockRange(),
  130. wrapping = range && prosemirrorTransform.findWrapping(range, nodeType, attrs);
  131. if (!wrapping) return null;
  132. tr.wrap(range, wrapping);
  133. var before = tr.doc.resolve(start - 1).nodeBefore;
  134. if (before && before.type == nodeType && prosemirrorTransform.canJoin(tr.doc, start - 1) && (!joinPredicate || joinPredicate(match, before))) tr.join(start - 1);
  135. return tr;
  136. });
  137. }
  138. function textblockTypeInputRule(regexp, nodeType) {
  139. var getAttrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  140. return new InputRule(regexp, function (state, match, start, end) {
  141. var $start = state.doc.resolve(start);
  142. var attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
  143. if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), nodeType)) return null;
  144. return state.tr["delete"](start, end).setBlockType(start, start, nodeType, attrs);
  145. });
  146. }
  147. exports.InputRule = InputRule;
  148. exports.closeDoubleQuote = closeDoubleQuote;
  149. exports.closeSingleQuote = closeSingleQuote;
  150. exports.ellipsis = ellipsis;
  151. exports.emDash = emDash;
  152. exports.inputRules = inputRules;
  153. exports.openDoubleQuote = openDoubleQuote;
  154. exports.openSingleQuote = openSingleQuote;
  155. exports.smartQuotes = smartQuotes;
  156. exports.textblockTypeInputRule = textblockTypeInputRule;
  157. exports.undoInputRule = undoInputRule;
  158. exports.wrappingInputRule = wrappingInputRule;