UNPKG

@atlaskit/editor-plugin-code-block

Version:

Code block plugin for @atlaskit/editor-core

200 lines (195 loc) 9.58 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { getBrowserInfo } from '@atlaskit/editor-common/browser'; import { codeBlockWrappedStates, defaultWordWrapState } from '@atlaskit/editor-common/code-block'; import { DOMSerializer } from '@atlaskit/editor-prosemirror/model'; import { fg } from '@atlaskit/platform-feature-flags'; import { resetShouldIgnoreFollowingMutations } from '../editor-commands'; import { getPluginState } from '../pm-plugins/main-state'; import { codeBlockClassNames } from '../ui/class-names'; // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp var MATCH_NEWLINES = new RegExp('\n', 'g'); var toDOM = function toDOM(node, contentEditable, formattedAriaLabel) { return ['div', { class: codeBlockClassNames.container }, ['div', { class: codeBlockClassNames.start, contenteditable: 'false' }], ['div', { class: codeBlockClassNames.contentWrapper }, ['div', { class: codeBlockClassNames.gutter, contenteditable: 'false' }], ['div', { class: codeBlockClassNames.content }, ['code', _objectSpread({ 'data-language': node.attrs.language || '', spellcheck: 'false', contenteditable: contentEditable ? 'true' : 'false', 'data-testid': 'code-block--code', 'aria-label': formattedAriaLabel }, fg('platform_editor_adf_with_localid') && { 'data-local-id': node.attrs.localId }), 0]]], ['div', { class: codeBlockClassNames.end, contenteditable: 'false' }]]; }; export var CodeBlockView = /*#__PURE__*/function () { function CodeBlockView(_node, view, getPos, formattedAriaLabel, api, cleanupEditorDisabledListener) { var _this = this, _api$editorDisabled; _classCallCheck(this, CodeBlockView); _defineProperty(this, "formattedAriaLabel", ''); /** * As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this. */ _defineProperty(this, "maintainDynamicGutterSize", function () { var totalLineCount = 1; _this.node.forEach(function (node) { var text = node.text; if (text) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion totalLineCount += (node.text.match(MATCH_NEWLINES) || []).length; } }); var maxDigits = totalLineCount.toString().length; _this.dom.style.setProperty('--lineNumberGutterWidth', "".concat(maxDigits, "ch")); }); this.cleanupEditorDisabledListener = cleanupEditorDisabledListener; var _DOMSerializer$render = DOMSerializer.renderSpec(document, toDOM(_node, !(api !== null && api !== void 0 && (_api$editorDisabled = api.editorDisabled) !== null && _api$editorDisabled !== void 0 && (_api$editorDisabled = _api$editorDisabled.sharedState.currentState()) !== null && _api$editorDisabled !== void 0 && _api$editorDisabled.editorDisabled), formattedAriaLabel)), dom = _DOMSerializer$render.dom, contentDOM = _DOMSerializer$render.contentDOM; this.getPos = getPos; this.view = view; this.node = _node; // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting this.dom = dom; // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting this.contentDOM = contentDOM; // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting this.lineNumberGutter = this.dom.querySelector(".".concat(codeBlockClassNames.gutter)); this.api = api; this.maintainDynamicGutterSize(); // Ensure the code block node has a wrapped state. // Wrapped state may already exist from breakout's recreating the node. if (!codeBlockWrappedStates.has(_node)) { codeBlockWrappedStates.set(_node, defaultWordWrapState); } this.handleEditorDisabledChanged(); } return _createClass(CodeBlockView, [{ key: "handleEditorDisabledChanged", value: function handleEditorDisabledChanged() { var _this$api, _this2 = this; if ((_this$api = this.api) !== null && _this$api !== void 0 && _this$api.editorDisabled) { this.cleanupEditorDisabledListener = this.api.editorDisabled.sharedState.onChange(function (sharedState) { if (_this2.contentDOM) { _this2.contentDOM.setAttribute('contenteditable', sharedState.nextSharedState.editorDisabled ? 'false' : 'true'); } }); } } }, { key: "updateDOMAndSelection", value: function updateDOMAndSelection(savedInnerHTML, newCursorPosition) { var _this$dom; if ((_this$dom = this.dom) !== null && _this$dom !== void 0 && _this$dom.childNodes && this.dom.childNodes.length > 1) { var _contentView$childNod; var contentWrapper = this.dom.childNodes[1]; var contentView = contentWrapper === null || contentWrapper === void 0 ? void 0 : contentWrapper.childNodes[1]; if ((contentView === null || contentView === void 0 || (_contentView$childNod = contentView.childNodes) === null || _contentView$childNod === void 0 ? void 0 : _contentView$childNod.length) > 0) { // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting var codeElement = contentView.firstChild; codeElement.innerHTML = savedInnerHTML; // We need to set cursor for the DOM update var textElement = Array.from(codeElement.childNodes).find(function (child) { return child.nodeName === '#text'; }); var sel = window.getSelection(); var range = document.createRange(); range.setStart(textElement, newCursorPosition); range.collapse(true); sel === null || sel === void 0 || sel.removeAllRanges(); sel === null || sel === void 0 || sel.addRange(range); } } } }, { key: "coalesceDOMElements", value: function coalesceDOMElements() { var _this$dom2; if ((_this$dom2 = this.dom) !== null && _this$dom2 !== void 0 && _this$dom2.childNodes && this.dom.childNodes.length > 1) { var contentWrapper = this.dom.childNodes[1]; var contentView = contentWrapper === null || contentWrapper === void 0 ? void 0 : contentWrapper.childNodes[1]; if (contentView !== null && contentView !== void 0 && contentView.childNodes && contentView.childNodes.length > 1) { var savedInnerHTML = ''; while (contentView.childNodes.length > 1) { // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting var lastChild = contentView.lastChild; savedInnerHTML = lastChild.innerHTML + savedInnerHTML; contentView.removeChild(lastChild); } // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting var firstChild = contentView.firstChild; savedInnerHTML = firstChild.innerHTML + '\n' + savedInnerHTML; var newCursorPosition = firstChild.innerHTML.length + 1; setTimeout(this.updateDOMAndSelection.bind(this, savedInnerHTML, newCursorPosition), 20); } } } }, { key: "update", value: function update(node) { if (node.type !== this.node.type) { return false; } if (node !== this.node) { if (node.attrs.language !== this.node.attrs.language) { this.contentDOM.setAttribute('data-language', node.attrs.language || ''); } this.node = node; this.maintainDynamicGutterSize(); var browser = getBrowserInfo(); if (browser.android) { this.coalesceDOMElements(); resetShouldIgnoreFollowingMutations(this.view.state, this.view.dispatch); } } return true; } }, { key: "ignoreMutation", value: function ignoreMutation(record) { var pluginState = getPluginState(this.view.state); if (pluginState !== null && pluginState !== void 0 && pluginState.shouldIgnoreFollowingMutations) { return true; } // Ensure updating the line-number gutter doesn't trigger reparsing the codeblock return record.target === this.lineNumberGutter || record.target.parentNode === this.lineNumberGutter; } }, { key: "destroy", value: function destroy() { if (this.cleanupEditorDisabledListener) { this.cleanupEditorDisabledListener(); } this.cleanupEditorDisabledListener = undefined; } }]); }(); export var codeBlockNodeView = function codeBlockNodeView(node, view, getPos, formattedAriaLabel, api) { return new CodeBlockView(node, view, getPos, formattedAriaLabel, api); };