@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
253 lines • 12.7 kB
JavaScript
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 { $isListItemNode, $isListNode, ListItemNode, ListNode, registerList, registerListStrictIndentTransform } from '@lexical/list';
import { $getNearestNodeOfType } from '@lexical/utils';
import { cx } from 'antd-style';
import { $isRootNode } 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 { listReplace } from "../utils";
import { registerCheckList } from "./checkList";
import { registerListCommands } from "./registry";
var ORDERED_LIST_REGEX = /^(\s*)(\d+)\.\s/;
var UNORDERED_LIST_REGEX = /^(\s*)[*+-]\s/;
var CHECK_LIST_REGEX = /^(\s*)(?:-\s)?\s?(\[(\s|x)?])\s/i;
export var ListPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
_inherits(ListPlugin, _KernelPlugin);
var _super = _createSuper(ListPlugin);
function ListPlugin(kernel, config) {
var _this;
_classCallCheck(this, ListPlugin);
_this = _super.call(this);
// Register the list nodes
_this.kernel = kernel;
_this.config = config;
kernel.registerNodes([ListNode, ListItemNode]);
// Register themes for list nodes
kernel.registerThemes({
// Define themes for list nodes here
list: {
checklist: 'editor_listItemCheck',
listitem: 'editor_listItem',
listitemChecked: 'editor_listItemChecked',
listitemUnchecked: 'editor_listItemUnchecked',
nested: {
listitem: 'editor_listItemNested'
},
ol: cx('editor_listOrdered', config === null || config === void 0 ? void 0 : config.theme),
olDepth: ['editor_listOrdered dp1', 'editor_listOrdered dp2', 'editor_listOrdered dp3', 'editor_listOrdered dp4', 'editor_listOrdered dp5'],
ul: cx('editor_listUnordered', config === null || config === void 0 ? void 0 : config.theme)
}
});
return _this;
}
_createClass(ListPlugin, [{
key: "onInit",
value: function onInit(editor) {
var _this$config;
this.register(registerList(editor));
this.register(registerCheckList(editor));
this.register(registerListStrictIndentTransform(editor));
this.register(registerListCommands(editor, this.kernel, {
enableHotkey: (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.enableHotkey
}));
this.registerMarkdown();
this.registerLiteXml();
}
}, {
key: "registerLiteXml",
value: function registerLiteXml() {
var litexmlService = this.kernel.requireService(ILitexmlService);
if (!litexmlService) {
return;
}
litexmlService.registerXMLWriter(ListNode.getType(), function (node, ctx) {
if ($isListNode(node)) {
var tagName = node.getListType() === 'number' ? 'ol' : 'ul';
var attributes = {};
if (node.getListType() === 'number' && node.getStart() !== 1) {
attributes.start = node.getStart().toString();
}
return ctx.createXmlNode(tagName, attributes);
}
return false;
});
litexmlService.registerXMLWriter(ListItemNode.getType(), function (node, ctx) {
if ($isListItemNode(node)) {
return ctx.createXmlNode('li');
}
return false;
});
litexmlService.registerXMLReader('ol', function (xmlNode, children) {
return INodeHelper.createElementNode('list', {
children: children,
direction: 'ltr',
format: '',
indent: 0,
listType: 'number',
start: xmlNode.getAttribute('start') ? parseInt(xmlNode.getAttribute('start'), 10) : 1,
tag: 'ol',
version: 1
});
});
litexmlService.registerXMLReader('ul', function (xmlNode, children) {
return INodeHelper.createElementNode('list', {
children: children,
direction: 'ltr',
format: '',
indent: 0,
listType: 'bullet',
start: 1,
tag: 'ul',
version: 1
});
});
litexmlService.registerXMLReader('li', function (xmlNode, children) {
return INodeHelper.createElementNode('listitem', {
children: children,
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
version: 1
});
});
}
}, {
key: "registerMarkdown",
value: function registerMarkdown() {
var markdownService = this.kernel.requireService(IMarkdownShortCutService);
if (!markdownService) {
return;
}
markdownService.registerMarkdownShortCut({
regExp: UNORDERED_LIST_REGEX,
replace: listReplace('bullet'),
type: 'element'
});
markdownService.registerMarkdownShortCut({
regExp: ORDERED_LIST_REGEX,
replace: listReplace('number'),
type: 'element'
});
markdownService.registerMarkdownShortCut({
regExp: CHECK_LIST_REGEX,
replace: listReplace('check'),
type: 'element'
});
markdownService.registerMarkdownWriter(ListNode.getType(), function (ctx, node) {
if ($isListNode(node)) {
ctx.wrap('', '\n');
}
});
var getLevel = function getLevel(node) {
if (!node) return 0;
if ($isRootNode(node.getParent())) {
return 0;
}
var parent = node.getParent();
if (!parent) {
return 0;
}
return getLevel($getNearestNodeOfType(parent, ListNode)) + 1;
};
markdownService.registerMarkdownWriter(ListItemNode.getType(), function (ctx, node) {
var parent = node.getParent();
if ($isListItemNode(node) && $isListNode(parent)) {
if ($isListNode(node.getFirstChild())) {
return;
}
var level = getLevel(parent);
var prefix = ' '.repeat(level);
switch (parent.getListType()) {
case 'bullet':
{
ctx.wrap(prefix + '- ', '\n');
break;
}
case 'number':
{
ctx.wrap("".concat(prefix).concat(node.getValue(), ". "), '\n');
break;
}
case 'check':
{
ctx.wrap("".concat(prefix, "- [").concat(node.getChecked() ? 'x' : ' ', "] "), '\n');
break;
}
default:
{
break;
}
}
}
});
markdownService.registerMarkdownReader('list', function (node, children) {
var _node$children, _node$children2;
var isCheck = ((_node$children = node.children) === null || _node$children === void 0 || (_node$children = _node$children[0]) === null || _node$children === void 0 ? void 0 : _node$children.type) === 'listItem' && typeof ((_node$children2 = node.children) === null || _node$children2 === void 0 || (_node$children2 = _node$children2[0]) === null || _node$children2 === void 0 ? void 0 : _node$children2.checked) === 'boolean';
var start = node.start || 1;
return INodeHelper.createElementNode('list', {
children: children.map(function (v) {
if (v.type === 'listitem') {
// @ts-expect-error not error
v.value = start++;
}
return v;
}),
direction: 'ltr',
format: '',
indent: 0,
listType: isCheck ? 'check' : node.ordered ? 'number' : 'bullet',
start: node.start || 1,
tag: node.ordered ? 'ol' : 'ul',
version: 1
});
});
markdownService.registerMarkdownReader('listItem', function (node, children, index) {
return children.map(function (v) {
if (v.type === 'paragraph') {
return INodeHelper.createElementNode('listitem', {
checked: typeof node.checked === 'boolean' ? node.checked : undefined,
// @ts-expect-error not error
children: v.children,
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
value: index + 1,
version: 1
});
} else if (v.type === 'list') {
return INodeHelper.createElementNode('listitem', {
children: [v],
direction: 'ltr',
format: '',
indent: 0,
type: 'listitem',
value: index + 1,
version: 1
});
}
// keep node unchanged
return v;
});
});
}
}]);
return ListPlugin;
}(KernelPlugin), _defineProperty(_class, "pluginName", 'ListPlugin'), _class);