HashtagNode 在 lexicaljs 中的正确使用方式是什么?

huangapple go评论68阅读模式
英文:

What is the proper way of using HashtagNode in lexicaljs?

问题

我正在尝试使用lexicaljs处理基本功能,如标签和提及等。

要使标签(hashtags)起作用,需要做什么呢?与Twitter上的工作方式相同,例如,不通过按钮触发,而是在键入#something时触发。

我修改了基本示例1,但在键入#...时什么都不发生。

有什么想法吗?

更新:似乎没有将类(样式)应用于标签节点,尽管它在树中被正确识别:

问题可能是什么?

英文:

I am trying to get basics to work with lexicaljs, like hashtags, mentions etc.

What does one need to do to get hashtag to work? Identical to how it works on Twitter for example, not through a button but triggered as one types #something.

Modified basic example, but nothing happens as one types #...

Any ideas?

import * as React from "react";
import { useRef } from "react";
import debounce from 'lodash.debounce';


import ExampleTheme from "./theme";
import { LexicalComposer } from "@lexical/react/LexicalComposer";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
import { ListItemNode, ListNode } from "@lexical/list";
import { CodeHighlightNode, CodeNode } from "@lexical/code";
import { AutoLinkNode, LinkNode } from "@lexical/link";
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
import { TRANSFORMERS } from "@lexical/markdown";
import { HashtagNode } from "@lexical/hashtag";

import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
import TreeViewPlugin from "./plugins/TreeViewPlugin";
import ToolbarPlugin from "./plugins/ToolbarPlugin";
import ListMaxIndentLevelPlugin from "./plugins/ListMaxIndentLevelPlugin";
import CodeHighlightPlugin from "./plugins/CodeHighlightPlugin";
import AutoLinkPlugin from "./plugins/AutoLinkPlugin";

import "./styles.css";

function Placeholder() {
	return <div className="editor-placeholder">Enter some rich text...</div>;
}

const editorConfig = {
	// The editor theme
	theme: ExampleTheme,
	// Handling of errors during update
	onError(error) {
		throw error;
	},
	// Any custom nodes go here
	nodes: [
		HeadingNode,
		ListNode,
		ListItemNode,
		QuoteNode,
		CodeNode,
		CodeHighlightNode,
		TableNode,
		TableCellNode,
		TableRowNode,
		AutoLinkNode,
		LinkNode,
		HashtagNode,
	]
};

export default function Editor(props) {

	function onChange(editorState) {
		if(props.onChange){
			props.onChange(editorState, JSON.stringify(editorState));
		}
	}

	const delayedOnChangeHandler = useRef(debounce((editorState) => {
        onChange(editorState)
    }, 1000)).current;

	return (
		<LexicalComposer initialConfig={editorConfig}>
			<div className="editor-container">
				<ToolbarPlugin />
				<div className="editor-inner">
					<RichTextPlugin
						contentEditable={<ContentEditable className="editor-input" />}
						placeholder={<Placeholder />}
						ErrorBoundary={LexicalErrorBoundary}
					/>
					<OnChangePlugin onChange={delayedOnChangeHandler}/>
					<HistoryPlugin />
					<TreeViewPlugin />
					<AutoFocusPlugin />
					<CodeHighlightPlugin />
					<ListPlugin />
					<LinkPlugin />
					<AutoLinkPlugin />
					<ListMaxIndentLevelPlugin maxDepth={7} />
					<MarkdownShortcutPlugin transformers={TRANSFORMERS} />
				</div>
			</div>
		</LexicalComposer>
	);
}

Update:
It appears that the class (style) is not being applied to the hashtag node, despite it being correctly recognized in the tree:

 root
  └ (1) paragraph  
    ├ (9) hashtag  "#hashtag"
    ├ (10) text  " "
>   └ (14) text  "bold" { format: bold }
>                 ^^^^
<p class="editor-paragraph ltr" dir="ltr">
    <span data-lexical-text="true">#hashtag</span>
    <span data-lexical-text="true"></span>
    <strong class="editor-text-bold" data-lexical-text="true">bold</strong>
</p>

What could be the problem?

答案1

得分: 3

已经检查了您的示例代码 - EditorTheme中的hashtag键应该是顶级键,而不是嵌套在text属性下。

英文:

Checked out your example code - the hashtag key in EditorTheme should be a top-level key, not nested under the text property.

答案2

得分: 0

以下是翻译好的部分:

"Popping another answer on here - as clickable hashtags (especially when readonly) is a common question. I've made some progress with Node Overrides, but frustratingly it's not actually doing the expected replacement. Ideas welcome... I'll update this if I figure it out. https://codesandbox.io/s/vigorous-jepsen-3i9ydu?file=/src/Editor.js"

英文:

Popping another answer on here - as clickable hashtags (especially when readonly) is a common question. I've made some progress with Node Overrides, but frustratingly it's not actually doing the expected replacement. Ideas welcome... I'll update this if I figure it out. https://codesandbox.io/s/vigorous-jepsen-3i9ydu?file=/src/Editor.js

huangapple
  • 本文由 发表于 2023年2月27日 08:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定