Rework MentionNode to take a whole Mention entity
This commit is contained in:
parent
70dc4caeb9
commit
e0c11fbfd1
2 changed files with 14 additions and 16 deletions
|
@ -16,28 +16,29 @@ import type {
|
||||||
SerializedLexicalNode,
|
SerializedLexicalNode,
|
||||||
Spread,
|
Spread,
|
||||||
} from 'lexical';
|
} from 'lexical';
|
||||||
|
import type { Mention as MentionEntity } from 'soapbox/schemas';
|
||||||
|
|
||||||
type SerializedMentionNode = Spread<{
|
type SerializedMentionNode = Spread<{
|
||||||
acct: string;
|
mention: MentionEntity;
|
||||||
type: 'mention';
|
type: 'mention';
|
||||||
version: 1;
|
version: 1;
|
||||||
}, SerializedLexicalNode>;
|
}, SerializedLexicalNode>;
|
||||||
|
|
||||||
class MentionNode extends DecoratorNode<JSX.Element> {
|
class MentionNode extends DecoratorNode<JSX.Element> {
|
||||||
|
|
||||||
__acct: string;
|
__mention: MentionEntity;
|
||||||
|
|
||||||
static getType(): string {
|
static getType(): string {
|
||||||
return 'mention';
|
return 'mention';
|
||||||
}
|
}
|
||||||
|
|
||||||
static clone(node: MentionNode): MentionNode {
|
static clone(node: MentionNode): MentionNode {
|
||||||
return new MentionNode(node.__acct, node.__key);
|
return new MentionNode(node.__mention, node.__key);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(acct: string, key?: NodeKey) {
|
constructor(mention: MentionEntity, key?: NodeKey) {
|
||||||
super(key);
|
super(key);
|
||||||
this.__acct = acct;
|
this.__mention = mention;
|
||||||
}
|
}
|
||||||
|
|
||||||
createDOM(config: EditorConfig): HTMLElement {
|
createDOM(config: EditorConfig): HTMLElement {
|
||||||
|
@ -49,20 +50,20 @@ class MentionNode extends DecoratorNode<JSX.Element> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static importJSON(serializedNode: SerializedMentionNode): MentionNode {
|
static importJSON(serializedNode: SerializedMentionNode): MentionNode {
|
||||||
const node = $createMentionNode(serializedNode.acct);
|
const node = $createMentionNode(serializedNode.mention);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
exportJSON(): SerializedMentionNode {
|
exportJSON(): SerializedMentionNode {
|
||||||
return {
|
return {
|
||||||
type: 'mention',
|
type: 'mention',
|
||||||
acct: this.__acct,
|
mention: this.__mention,
|
||||||
version: 1,
|
version: 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getTextContent(): string {
|
getTextContent(): string {
|
||||||
return `@${this.__acct}`;
|
return `@${this.__mention.acct}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
canInsertTextBefore(): boolean {
|
canInsertTextBefore(): boolean {
|
||||||
|
@ -74,18 +75,15 @@ class MentionNode extends DecoratorNode<JSX.Element> {
|
||||||
}
|
}
|
||||||
|
|
||||||
decorate(): JSX.Element {
|
decorate(): JSX.Element {
|
||||||
const acct = this.__acct;
|
|
||||||
const username = acct.split('@')[0];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Mention mention={{ acct, username }} disabled />
|
<Mention mention={this.__mention} disabled />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function $createMentionNode(acct: string): MentionNode {
|
function $createMentionNode(mention: MentionEntity): MentionNode {
|
||||||
const node = new MentionNode(acct);
|
const node = new MentionNode(mention);
|
||||||
return $applyNodeReplacement(node);
|
return $applyNodeReplacement(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -327,8 +327,8 @@ const AutosuggestPlugin = ({
|
||||||
node.setTextContent(`${suggestion} `);
|
node.setTextContent(`${suggestion} `);
|
||||||
node.select();
|
node.select();
|
||||||
} else {
|
} else {
|
||||||
const acct = selectAccount(getState(), suggestion)!.acct;
|
const account = selectAccount(getState(), suggestion)!;
|
||||||
replaceMatch($createMentionNode(acct));
|
replaceMatch($createMentionNode(account));
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(clearComposeSuggestions(composeId));
|
dispatch(clearComposeSuggestions(composeId));
|
||||||
|
|
Loading…
Reference in a new issue