@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
103 lines (101 loc) • 8.21 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 { INodeHelper } from "../../../editor-kernel/inode/helper";
import { KernelPlugin } from "../../../editor-kernel/plugin";
import { ILitexmlService } from "../../litexml";
import { IMarkdownShortCutService, MARKDOWN_READER_LEVEL_HIGH } from "../../markdown/service/shortcut";
import { registerMentionCommand } from "../command";
import { $isMentionNode, MentionNode } from "../node/MentionNode";
import { registerMentionNodeSelectionObserver } from "./register";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export var MentionPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
_inherits(MentionPlugin, _KernelPlugin);
var _super = _createSuper(MentionPlugin);
function MentionPlugin(kernel, config) {
var _this;
_classCallCheck(this, MentionPlugin);
_this = _super.call(this);
// Register the file node
_this.kernel = kernel;
_this.config = config;
kernel.registerNodes([MentionNode]);
if (config !== null && config !== void 0 && config.theme) {
kernel.registerThemes(config === null || config === void 0 ? void 0 : config.theme);
}
_this.registerDecorator(kernel, MentionNode.getType(), function (node, editor) {
return config !== null && config !== void 0 && config.decorator ? config.decorator(node, editor) : null;
});
return _this;
}
_createClass(MentionPlugin, [{
key: "onInit",
value: function onInit(editor) {
this.register(registerMentionCommand(editor));
this.register(registerMentionNodeSelectionObserver(editor));
this.registerMarkdown();
this.registerLiteXml();
}
}, {
key: "registerLiteXml",
value: function registerLiteXml() {
var _this$kernel$requireS, _this$kernel$requireS2;
(_this$kernel$requireS = this.kernel.requireService(ILitexmlService)) === null || _this$kernel$requireS === void 0 || _this$kernel$requireS.registerXMLWriter(MentionNode.getType(), function (node, ctx) {
if ($isMentionNode(node)) {
var attributes = {
label: node.label
};
if (node.metadata && Object.keys(node.metadata).length > 0) {
attributes.metadata = JSON.stringify(node.metadata);
}
return ctx.createXmlNode('mention', attributes);
}
return false;
});
(_this$kernel$requireS2 = this.kernel.requireService(ILitexmlService)) === null || _this$kernel$requireS2 === void 0 || _this$kernel$requireS2.registerXMLReader('mention', function (xmlNode) {
return INodeHelper.createElementNode(MentionNode.getType(), {
label: xmlNode.getAttribute('label') || '',
metadata: xmlNode.getAttribute('metadata') ? JSON.parse(xmlNode.getAttribute('metadata')) : {}
});
});
}
}, {
key: "registerMarkdown",
value: function registerMarkdown() {
var _this$kernel$requireS3,
_this2 = this,
_this$config;
(_this$kernel$requireS3 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS3 === void 0 || _this$kernel$requireS3.registerMarkdownWriter(MentionNode.getType(), function (ctx, node) {
if ($isMentionNode(node)) {
var _this2$config;
if ((_this2$config = _this2.config) !== null && _this2$config !== void 0 && _this2$config.markdownWriter) {
ctx.appendLine(_this2.config.markdownWriter(node));
return;
}
ctx.appendLine("".concat(node.label));
}
});
if ((_this$config = this.config) !== null && _this$config !== void 0 && _this$config.markdownReader) {
var _this$kernel$requireS4;
(_this$kernel$requireS4 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS4 === void 0 || _this$kernel$requireS4.registerMarkdownReader('html', function (node, children) {
var _this2$config2;
return (_this2$config2 = _this2.config) !== null && _this2$config2 !== void 0 && _this2$config2.markdownReader ? _this2.config.markdownReader(node, children) || false : false;
// return this.config?.markdownReader ? this.config.markdownReader(node, children) || false : false;
}, MARKDOWN_READER_LEVEL_HIGH);
}
}
}]);
return MentionPlugin;
}(KernelPlugin), _defineProperty(_class, "pluginName", 'MentionPlugin'), _class);