UNPKG

@lobehub/editor

Version:

A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.

229 lines 12.1 kB
var _class; 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); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { $createCodeNode, $isCodeHighlightNode, $isCodeNode, CodeHighlightNode, CodeNode } from '@lexical/code'; import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, PASTE_COMMAND, TabNode } from 'lexical'; import { INodeHelper } from "../../../editor-kernel/inode/helper"; import { KernelPlugin } from "../../../editor-kernel/plugin"; import { ILitexmlService } from "../../litexml"; import { IMarkdownShortCutService } from "../../markdown/service/shortcut"; import { CustomShikiTokenizer, registerCodeCommand } from "../command"; import { getCodeLanguageByInput } from "../utils/language"; import { registerCodeHighlighting, toCodeTheme } from "./CodeHighlighterShiki"; var origin = CodeNode.importDOM; var LANGUAGE_DATA_ATTRIBUTE = 'data-language'; var MAX_LENGTH = 8000; function $convertPreElement(domNode) { var language = domNode.getAttribute(LANGUAGE_DATA_ATTRIBUTE); if (domNode.innerHTML.length > MAX_LENGTH) { return { node: null }; } return { node: $createCodeNode(language) }; } CodeNode.importDOM = function () { var node = origin(); if (node) { node.pre = function () { return { conversion: $convertPreElement, priority: 0 }; }; } // Custom logic for handling imported DOM return node; }; function toMarkdownTheme(theme) { if (!theme) { return ''; } if (typeof theme === 'string') { return theme; } return "".concat(theme.light, " ").concat(theme.dark); } export var CodeblockPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) { _inherits(CodeblockPlugin, _KernelPlugin); var _super = _createSuper(CodeblockPlugin); function CodeblockPlugin(kernel) { var _config$theme, _this$config; var _this; var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, CodeblockPlugin); _this = _super.call(this); // Register the code block plugin _this.kernel = kernel; _this.config = config; kernel.registerNodes([CodeNode, CodeHighlightNode]); kernel.registerThemes({ code: ((_config$theme = config.theme) === null || _config$theme === void 0 ? void 0 : _config$theme.code) || 'editor-code' }); if ((_this$config = _this.config) !== null && _this$config !== void 0 && _this$config.shikiTheme) { var _this$config2; CustomShikiTokenizer.defaultTheme = (_this$config2 = _this.config) === null || _this$config2 === void 0 ? void 0 : _this$config2.shikiTheme; } return _this; } _createClass(CodeblockPlugin, [{ key: "onInit", value: function onInit(editor) { var _this$config3; if ((_this$config3 = this.config) !== null && _this$config3 !== void 0 && _this$config3.shikiTheme) { this.register(registerCodeHighlighting(editor, CustomShikiTokenizer)); } else { this.register(registerCodeHighlighting(editor)); } this.register(registerCodeCommand(editor)); this.register(editor.registerCommand(PASTE_COMMAND, function (event) { if (!(event instanceof ClipboardEvent)) return false; var clipboardData = event.clipboardData; if (!clipboardData) return false; var isInHighlightNodeSelection = editor.getEditorState().read(function () { var sel = $getSelection(); if (!$isRangeSelection(sel)) { return false; } var node = sel.focus.getNode(); while (node) { if ($isCodeHighlightNode(node) || $isCodeNode(node)) { return sel; } node = node.getParent(); } return false; }); if (isInHighlightNodeSelection) { var rawText = clipboardData.getData('text/plain').trimEnd() || clipboardData.getData('text/uri-list').trimEnd(); editor.update(function () { isInHighlightNodeSelection.insertRawText(rawText); }); return true; } return false; }, COMMAND_PRIORITY_EDITOR)); this.registerMarkdown(); this.registerLiteXml(); } }, { key: "registerLiteXml", value: function registerLiteXml() { var _this2 = this; var litexmlService = this.kernel.requireService(ILitexmlService); if (!litexmlService) { return; } litexmlService.registerXMLWriter(CodeNode.getType(), function (node, ctx) { var codeNode = node; var xmlNode = ctx.createXmlNode('code', { lang: codeNode.getLanguage() || 'plaintext' }, codeNode.getTextContent()); return xmlNode; }); litexmlService.registerXMLReader('code', function (xmlElment, children) { var _this2$config; var text = children.map(function (v) { return v.text || ''; }).join(''); return INodeHelper.createElementNode(CodeNode.getType(), { children: text.split('\n').flatMap(function (text, index, arr) { var textNode = INodeHelper.createTextNode(text); textNode.type = 'code-highlight'; if (index === arr.length - 1) { return textNode; } return [textNode, { type: 'linebreak', version: 1 }]; }).flat(), direction: 'ltr', format: '', indent: 0, language: xmlElment.getAttribute('lang'), textStyle: '--shiki-dark:var(--color-info);--shiki-light:var(--color-info)', theme: "".concat(toMarkdownTheme((_this2$config = _this2.config) === null || _this2$config === void 0 ? void 0 : _this2$config.shikiTheme), " needUpdate"), version: 1 }); }); } }, { key: "registerMarkdown", value: function registerMarkdown() { var _this3 = this; var kernel = this.kernel; var markdownService = kernel.requireService(IMarkdownShortCutService); if (!markdownService) { return; } markdownService.registerMarkdownShortCut({ regExp: /^(```|···)(.+)?$/, replace: function replace(parentNode, _, match) { var code = $createCodeNode(getCodeLanguageByInput(match[2]), toCodeTheme(CustomShikiTokenizer)); parentNode.replace(code); code.selectStart(); }, trigger: 'enter', type: 'element' }); markdownService.registerMarkdownWriter(CodeNode.getType(), function (ctx, node) { if ($isCodeNode(node)) { ctx.wrap('```' + (node.getLanguage() || '') + '\n', '\n```\n'); } }); markdownService.registerMarkdownWriter(TabNode.getType(), function (ctx) { ctx.appendLine(' '); }); markdownService.registerMarkdownWriter(CodeHighlightNode.getType(), function (ctx, node) { if ($isCodeHighlightNode(node)) { ctx.appendLine(node.getTextContent()); } }); markdownService.registerMarkdownWriter('linebreak', function (ctx, node) { if ($isCodeNode(node.getParent())) { ctx.appendLine('\n'); } }); markdownService.registerMarkdownReader('code', function (node) { var _this3$config; var language = node.lang ? getCodeLanguageByInput(node.lang) : 'plaintext'; return INodeHelper.createElementNode('code', { children: node.value.split('\n').flatMap(function (text, index, arr) { var textNode = INodeHelper.createTextNode(text); textNode.type = 'code-highlight'; if (index === arr.length - 1) { return textNode; } return [textNode, { type: 'linebreak', version: 1 }]; }).flat(), direction: 'ltr', format: '', indent: 0, language: language, textStyle: '--shiki-dark:var(--color-info);--shiki-light:var(--color-info)', theme: "".concat(toMarkdownTheme((_this3$config = _this3.config) === null || _this3$config === void 0 ? void 0 : _this3$config.shikiTheme), " needUpdate"), version: 1 }); }); } }]); return CodeblockPlugin; }(KernelPlugin), _defineProperty(_class, "pluginName", 'CodeblockPlugin'), _class);