UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

134 lines (130 loc) 7.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _keymap = require("@atlaskit/editor-prosemirror/keymap"); var _state = require("@atlaskit/editor-prosemirror/state"); var _utils2 = require("@atlaskit/editor-prosemirror/utils"); var _bracketHandling = require("./ide-ux/bracket-handling"); var _commands = require("./ide-ux/commands"); var _lineHandling = require("./ide-ux/line-handling"); var _pairedCharacterHandling = require("./ide-ux/paired-character-handling"); var _quoteHandling = require("./ide-ux/quote-handling"); var _utils3 = require("./utils"); var ideUX = function ideUX(pluginInjectionApi) { var _pluginInjectionApi$a; var editorAnalyticsAPI = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a === void 0 ? void 0 : _pluginInjectionApi$a.actions; return new _safePlugin.SafePlugin({ props: { handleTextInput: function handleTextInput(view, from, to, text) { var _pluginInjectionApi$c; var state = view.state, dispatch = view.dispatch; var compositionPluginState = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$c = pluginInjectionApi.composition) === null || _pluginInjectionApi$c === void 0 ? void 0 : _pluginInjectionApi$c.sharedState.currentState(); if ((0, _lineHandling.isCursorInsideCodeBlock)(state) && !(compositionPluginState !== null && compositionPluginState !== void 0 && compositionPluginState.isComposing)) { var beforeText = (0, _lineHandling.getStartOfCurrentLine)(state).text; var afterText = (0, _lineHandling.getEndOfCurrentLine)(state).text; // If text is a closing bracket/quote and we've already inserted it, move the selection after if ((0, _pairedCharacterHandling.isCursorBeforeClosingCharacter)(afterText) && (0, _pairedCharacterHandling.isClosingCharacter)(text) && afterText.startsWith(text)) { dispatch((0, _utils2.setTextSelection)(to + text.length)(state.tr)); return true; } // Automatically add right-hand side bracket when user types the left bracket if ((0, _bracketHandling.shouldAutoCloseBracket)(beforeText, afterText)) { var _getAutoClosingBracke = (0, _bracketHandling.getAutoClosingBracketInfo)(beforeText + text, afterText), left = _getAutoClosingBracke.left, right = _getAutoClosingBracke.right; if (left && right) { var bracketPair = state.schema.text(text + right); var tr = state.tr.replaceWith(from, to, bracketPair); dispatch((0, _utils2.setTextSelection)(from + text.length)(tr)); return true; } } // Automatically add closing quote when user types a starting quote if ((0, _quoteHandling.shouldAutoCloseQuote)(beforeText, afterText)) { var _getAutoClosingQuoteI = (0, _quoteHandling.getAutoClosingQuoteInfo)(beforeText + text, afterText), leftQuote = _getAutoClosingQuoteI.left, rightQuote = _getAutoClosingQuoteI.right; if (leftQuote && rightQuote) { var quotePair = state.schema.text(text + rightQuote); var _tr = state.tr.replaceWith(from, to, quotePair); dispatch((0, _utils2.setTextSelection)(from + text.length)(_tr)); return true; } } } return false; }, handleKeyDown: (0, _keymap.keydownHandler)({ Backspace: function Backspace(state, dispatch) { if ((0, _lineHandling.isCursorInsideCodeBlock)(state)) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var $cursor = (0, _utils3.getCursor)(state.selection); var beforeText = (0, _lineHandling.getStartOfCurrentLine)(state).text; var afterText = (0, _lineHandling.getEndOfCurrentLine)(state).text; var _getAutoClosingBracke2 = (0, _bracketHandling.getAutoClosingBracketInfo)(beforeText, afterText), leftBracket = _getAutoClosingBracke2.left, rightBracket = _getAutoClosingBracke2.right, hasTrailingMatchingBracket = _getAutoClosingBracke2.hasTrailingMatchingBracket; if (leftBracket && rightBracket && hasTrailingMatchingBracket && dispatch) { dispatch(state.tr.delete($cursor.pos - leftBracket.length, $cursor.pos + rightBracket.length)); return true; } var _getAutoClosingQuoteI2 = (0, _quoteHandling.getAutoClosingQuoteInfo)(beforeText, afterText), leftQuote = _getAutoClosingQuoteI2.left, rightQuote = _getAutoClosingQuoteI2.right, hasTrailingMatchingQuote = _getAutoClosingQuoteI2.hasTrailingMatchingQuote; if (leftQuote && rightQuote && hasTrailingMatchingQuote && dispatch) { dispatch(state.tr.delete($cursor.pos - leftQuote.length, $cursor.pos + rightQuote.length)); return true; } var _getLineInfo = (0, _lineHandling.getLineInfo)(beforeText), _getLineInfo$indentTo = _getLineInfo.indentToken, size = _getLineInfo$indentTo.size, token = _getLineInfo$indentTo.token, indentText = _getLineInfo.indentText; if (beforeText === indentText) { if (indentText.endsWith(token.repeat(size)) && dispatch) { dispatch(state.tr.delete($cursor.pos - (size - indentText.length % size || size), $cursor.pos)); return true; } } } return false; }, Enter: (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, _commands.insertNewlineWithIndent), 'Mod-]': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.indent)(editorAnalyticsAPI)), 'Mod-[': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.outdent)(editorAnalyticsAPI)), Tab: (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, function (state, dispatch) { if (!dispatch) { return false; } if ((0, _lineHandling.isCursorInsideCodeBlock)(state)) { return (0, _commands.insertIndent)(state, dispatch); } return (0, _commands.indent)(editorAnalyticsAPI)(state, dispatch); }), 'Shift-Tab': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.outdent)(editorAnalyticsAPI)), 'Mod-a': function ModA(state, dispatch) { if ((0, _lineHandling.isSelectionEntirelyInsideCodeBlock)(state)) { var _state$selection = state.selection, $from = _state$selection.$from, $to = _state$selection.$to; var isFullCodeBlockSelection = $from.parentOffset === 0 && $to.parentOffset === $to.parent.nodeSize - 2; if (!isFullCodeBlockSelection && dispatch) { dispatch(state.tr.setSelection(_state.TextSelection.create(state.doc, $from.start(), $to.end()))); return true; } } return false; } }) } }); }; var _default = exports.default = ideUX;