-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
16 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/ui/src/auto-form/fields/markdown/TokenContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { createContext, PropsWithChildren, useContext } from "react"; | ||
|
||
export type TokenContext = { | ||
staticTokens: string[]; | ||
dynamicTokens: RegExp | null; | ||
}; | ||
|
||
export const TokenContext = createContext<TokenContext>({ | ||
staticTokens: [], | ||
dynamicTokens: /^.$/, | ||
}); | ||
|
||
export const useTokenContext = () => { | ||
return useContext(TokenContext); | ||
}; | ||
|
||
export function TokenProvider(props: PropsWithChildren<TokenContext>) { | ||
return <TokenContext.Provider value={props}>{props.children}</TokenContext.Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { EditorConfig, LexicalNode, NodeKey, SerializedTextNode } from "lexical"; | ||
|
||
import { addClassNamesToElement } from "@lexical/utils"; | ||
import { $applyNodeReplacement, TextNode } from "lexical"; | ||
|
||
export class TokenNode extends TextNode { | ||
static getType(): string { | ||
return "token"; | ||
} | ||
|
||
static clone(node: TokenNode): TokenNode { | ||
return new TokenNode(node.__text, node.__key); | ||
} | ||
|
||
constructor(text: string, key?: NodeKey) { | ||
super(text, key); | ||
console.log(text, key); | ||
} | ||
|
||
createDOM(config: EditorConfig): HTMLElement { | ||
const element = super.createDOM(config); | ||
addClassNamesToElement(element, config.theme.token); | ||
return element; | ||
} | ||
|
||
static importJSON(serializedNode: SerializedTextNode): TokenNode { | ||
const node = $createTokenNode(serializedNode.text); | ||
node.setFormat(serializedNode.format); | ||
node.setDetail(serializedNode.detail); | ||
node.setMode(serializedNode.mode); | ||
node.setStyle(serializedNode.style); | ||
return node; | ||
} | ||
|
||
exportJSON(): SerializedTextNode { | ||
return { | ||
...super.exportJSON(), | ||
type: "token", | ||
}; | ||
} | ||
|
||
canInsertTextBefore(): boolean { | ||
return false; | ||
} | ||
|
||
isTextEntity(): true { | ||
return true; | ||
} | ||
} | ||
|
||
export function $createTokenNode(text = ""): TokenNode { | ||
return $applyNodeReplacement(new TokenNode(text)); | ||
} | ||
|
||
export function $isTokenNode(node: LexicalNode | null | undefined): node is TokenNode { | ||
return node instanceof TokenNode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useEffect, useMemo } from "react"; | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
import { useLexicalTextEntity } from "@lexical/react/useLexicalTextEntity"; | ||
import { TextNode } from "lexical"; | ||
|
||
import { useTokenContext } from "./TokenContext"; | ||
import { $createTokenNode, TokenNode } from "./TokenNode"; | ||
|
||
const boundary = "^|$|[^&/" + "*" + "]"; | ||
|
||
const $createTokenNode_ = (textNode: TextNode): TokenNode => { | ||
return $createTokenNode(textNode.getTextContent()); | ||
}; | ||
|
||
export function TokenPlugin() { | ||
const [editor] = useLexicalComposerContext(); | ||
const { staticTokens, dynamicTokens } = useTokenContext(); | ||
|
||
const REGEX = useMemo( | ||
() => new RegExp(`(${boundary})\{(${staticTokens.join("|")})\}`, "i"), | ||
[staticTokens] | ||
); | ||
|
||
useEffect(() => { | ||
if (!editor.hasNodes([TokenNode])) { | ||
throw new Error("TokenPlugin: TokenNode not registered on editor"); | ||
} | ||
}, [editor]); | ||
|
||
const getTokenMatch = useCallback((text: string) => { | ||
const matchArr = REGEX.exec(text); | ||
|
||
if (matchArr === null) { | ||
return null; | ||
} | ||
|
||
const tokenLength = matchArr[2].length + 2; // add two for the curly braces | ||
const startOffset = matchArr.index + matchArr[1].length; | ||
const endOffset = startOffset + tokenLength; | ||
|
||
return { | ||
end: endOffset, | ||
start: startOffset, | ||
}; | ||
}, []); | ||
|
||
useLexicalTextEntity<TokenNode>(getTokenMatch, TokenNode, $createTokenNode_); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.