Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
6.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {ComposerAtomicPresentation} from '@app/features/lexical/composer/nodes/ComposerAtomicPresentation';
import {ComposerAtomicTokenNode} from '@app/features/lexical/composer/nodes/ComposerAtomicTokenNode';
import styles from '@app/features/lexical/composer/nodes/ComposerInline.module.css';
import {ComposerMentionPill} from '@app/features/lexical/composer/nodes/ComposerMentionPill';
import type {MentionSegment} from '@app/features/messaging/utils/TextareaSegmentManager';
import type {DOMExportOutput, EditorConfig, LexicalNode, NodeKey, SerializedLexicalNode, Spread} from 'lexical';
import type {CSSProperties, JSX} from 'react';

export type ComposerMentionType = 'user' | 'role' | 'channel' | 'special';

export const ComposerMentionPresentation = {
	none: 0,
	italic: 1 << 0,
	underline: 1 << 1,
	strike: 1 << 2,
	subtext: 1 << 3,
} as const;

export type ComposerMentionPresentationFormat = number;

export type SerializedComposerMentionNode = Spread<
	{
		mentionType: ComposerMentionType;
		mentionId: string;
		display: string;
		wire: string;
		literal: boolean;
		spoiler: boolean;
		presentation?: ComposerMentionPresentationFormat;
	},
	SerializedLexicalNode
>;

export class ComposerMentionNode extends ComposerAtomicTokenNode {
	__mentionType: ComposerMentionType;
	__mentionId: string;
	__wire: string;

	static override getType(): string {
		return 'composer-mention';
	}

	static override clone(node: ComposerMentionNode): ComposerMentionNode {
		return new ComposerMentionNode(
			node.__mentionType,
			node.__mentionId,
			node.__display,
			node.__wire,
			node.__literal,
			node.__spoiler,
			node.__presentation,
			node.__key,
		);
	}

	static override importJSON(serializedNode: SerializedComposerMentionNode): ComposerMentionNode {
		return $createComposerMentionNode(
			serializedNode.mentionType,
			serializedNode.mentionId,
			serializedNode.display,
			serializedNode.wire,
			serializedNode.literal == null ? false : serializedNode.literal,
			serializedNode.spoiler == null ? false : serializedNode.spoiler,
			serializedNode.presentation == null ? ComposerMentionPresentation.none : serializedNode.presentation,
		);
	}

	__presentation: ComposerMentionPresentationFormat;

	constructor(
		mentionType: ComposerMentionType,
		mentionId: string,
		display: string,
		wire: string,
		literal = false,
		spoiler = false,
		presentation: ComposerMentionPresentationFormat = ComposerMentionPresentation.none,
		key?: NodeKey,
	) {
		super(display, literal, spoiler, key);
		this.__mentionType = mentionType;
		this.__mentionId = mentionId;
		this.__wire = wire;
		this.__presentation = presentation;
	}

	override exportJSON(): SerializedComposerMentionNode {
		const serialized: SerializedComposerMentionNode = {
			...super.exportJSON(),
			mentionType: this.__mentionType,
			mentionId: this.__mentionId,
			display: this.__display,
			wire: this.__wire,
			literal: this.__literal,
			spoiler: this.__spoiler,
		};
		if (this.__presentation !== ComposerMentionPresentation.none) {
			serialized.presentation = this.__presentation;
		}
		return serialized;
	}

	override createDOM(config: EditorConfig): HTMLElement {
		const span = document.createElement('span');
		const className = config.theme.composerMention;
		if (typeof className === 'string') {
			span.className = className;
		}
		span.setAttribute('data-lexical-composer-mention', this.__mentionType);
		span.spellcheck = false;
		return span;
	}

	override exportDOM(): DOMExportOutput {
		const element = document.createElement('span');
		element.setAttribute('data-lexical-composer-mention', this.__mentionType);
		element.textContent = this.__wire;
		return {element};
	}

	getWireText(): string {
		return this.getLatest().__wire;
	}

	getSegmentType(): MentionSegment['type'] {
		return this.getLatest().__mentionType;
	}

	getMentionId(): string {
		return this.getLatest().__mentionId;
	}

	getPresentation(): ComposerMentionPresentationFormat {
		return this.getLatest().__presentation;
	}

	setPresentation(presentation: ComposerMentionPresentationFormat): this {
		this.getWritable().__presentation = presentation;
		return this;
	}

	override decorate(): JSX.Element {
		const presentationStyle = mentionPresentationStyle(this.__presentation);
		return (
			<ComposerAtomicPresentation
				spoiler={this.__spoiler}
				data-flx="lexical.composer.nodes.composer-mention-node.composer-atomic-presentation"
			>
				<span style={presentationStyle} data-flx="lexical.composer.nodes.composer-mention-node.span">
					{this.__literal ? (
						<span className={styles.literal} data-flx="lexical.composer.nodes.composer-mention-node.literal">
							{this.__wire}
						</span>
					) : (
						<ComposerMentionPill
							mentionType={this.__mentionType}
							mentionId={this.__mentionId}
							display={this.__display}
							data-flx="lexical.composer.nodes.composer-mention-node.composer-mention-pill"
						/>
					)}
				</span>
			</ComposerAtomicPresentation>
		);
	}
}

function mentionPresentationStyle(presentation: ComposerMentionPresentationFormat): CSSProperties | undefined {
	if (presentation === ComposerMentionPresentation.none) {
		return undefined;
	}
	const underline = (presentation & ComposerMentionPresentation.underline) !== 0;
	const strike = (presentation & ComposerMentionPresentation.strike) !== 0;
	const textDecorations: Array<string> = [];
	if (underline) {
		textDecorations.push('underline');
	}
	if (strike) {
		textDecorations.push('line-through');
	}
	return {
		fontStyle: (presentation & ComposerMentionPresentation.italic) !== 0 ? 'italic' : undefined,
		textDecorationLine: textDecorations.length > 0 ? textDecorations.join(' ') : undefined,
		fontSize: (presentation & ComposerMentionPresentation.subtext) !== 0 ? '0.85em' : undefined,
		color:
			(presentation & ComposerMentionPresentation.subtext) !== 0
				? 'var(--text-muted,var(--text-secondary))'
				: undefined,
	};
}

export function $createComposerMentionNode(
	mentionType: ComposerMentionType,
	mentionId: string,
	display: string,
	wire: string,
	literal = false,
	spoiler = false,
	presentation: ComposerMentionPresentationFormat = ComposerMentionPresentation.none,
): ComposerMentionNode {
	return new ComposerMentionNode(mentionType, mentionId, display, wire, literal, spoiler, presentation);
}

export function $isComposerMentionNode(node: LexicalNode | null | undefined): node is ComposerMentionNode {
	return node instanceof ComposerMentionNode;
}