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.

179 lines (174 loc) 8.43 kB
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 _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); } 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); } /* eslint-disable @typescript-eslint/no-use-before-define */ import { addClassNamesToElement } from '@lexical/utils'; import { $applyNodeReplacement, $getSelection, $isNodeSelection, $isRangeSelection, ElementNode } from 'lexical'; import { createDebugLogger } from "../../../utils/debug"; var logger = createDebugLogger('plugin', 'link-highlight'); export var LinkHighlightNode = /*#__PURE__*/function (_ElementNode) { _inherits(LinkHighlightNode, _ElementNode); var _super = _createSuper(LinkHighlightNode); function LinkHighlightNode() { _classCallCheck(this, LinkHighlightNode); return _super.apply(this, arguments); } _createClass(LinkHighlightNode, [{ key: "createDOM", value: function createDOM(config) { logger.debug('🔍 config', config); var element = document.createElement('a'); // Set link attributes var url = this.getURL(); if (url) { element.href = this.formatUrl(url); } element.setAttribute('target', '_blank'); element.setAttribute('rel', 'noopener noreferrer'); addClassNamesToElement(element, config.theme.linkHighlight); return element; } /** * Format URL to ensure it has proper protocol */ }, { key: "formatUrl", value: function formatUrl(url) { // Check if URL already has a protocol if (/^[a-z][\d+.a-z-]*:/i.test(url)) { return url; } // Check if it's a relative path if (/^[#./]/.test(url)) { return url; } // Check for email address if (url.includes('@') && !url.startsWith('mailto:')) { return "mailto:".concat(url); } // Check for phone number if (/^\+?[\d\s()-]{5,}$/.test(url)) { return "tel:".concat(url); } // Default to https if (url.startsWith('www.')) { return "https://".concat(url); } // If no protocol, assume https if (!url.includes('://')) { return "https://".concat(url); } return url; } }, { key: "canBeEmpty", value: function canBeEmpty() { return false; } }, { key: "isInline", value: function isInline() { return true; } }, { key: "canInsertTextBefore", value: function canInsertTextBefore() { return false; } }, { key: "canInsertTextAfter", value: function canInsertTextAfter() { return false; } }, { key: "updateDOM", value: function updateDOM(prevNode, dom, config) { // Update the class names if theme has changed var prevTheme = prevNode ? prevNode : null; if (prevTheme !== this) { addClassNamesToElement(dom, config.theme.linkHighlight); } // Update href attribute if it's an anchor element if (dom instanceof HTMLAnchorElement) { var url = this.getURL(); if (url) { dom.href = this.formatUrl(url); } else { dom.removeAttribute('href'); } } return false; } /** * Get the URL from the text content */ }, { key: "getURL", value: function getURL() { return this.getTextContent().trim(); } }], [{ key: "getType", value: function getType() { return 'linkHighlight'; } }, { key: "clone", value: function clone(node) { return new LinkHighlightNode(node.__key); } }, { key: "importJSON", value: function importJSON(serializedNode) { return $createLinkHighlightNode().updateFromJSON(serializedNode); } }]); return LinkHighlightNode; }(ElementNode); export function $createLinkHighlightNode() { return $applyNodeReplacement(new LinkHighlightNode()); } export function $isLinkHighlightNode(node) { return node instanceof LinkHighlightNode; } export function getLinkHighlightNode(node) { if ($isLinkHighlightNode(node)) { return node; } var parent = node.getParent(); if ($isLinkHighlightNode(parent)) { return parent; } return null; } export function $isSelectionInLinkHighlight(editor) { return editor.getEditorState().read(function () { var selection = $getSelection(); if (!selection) { return false; } if ($isRangeSelection(selection)) { var focusNode = selection.focus.getNode(); var anchorNode = selection.anchor.getNode(); var focusLinkHighlight = getLinkHighlightNode(focusNode); var anchorLinkHighlight = getLinkHighlightNode(anchorNode); // Both nodes should be in the same link-highlight node return focusLinkHighlight !== null && anchorLinkHighlight !== null && focusLinkHighlight === anchorLinkHighlight; } if ($isNodeSelection(selection)) { var nodes = selection.getNodes(); return nodes.length === 1 && $isLinkHighlightNode(nodes[0]); } return false; }); }