-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Mirascope/refine-first-version
Refine first version
- Loading branch information
Showing
34 changed files
with
741 additions
and
197 deletions.
There are no files selected for viewing
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
82 changes: 44 additions & 38 deletions
82
client/src/components/lexical/template-auto-replace-plugin.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 |
---|---|---|
@@ -1,57 +1,63 @@ | ||
import { TextNode } from "lexical"; | ||
import { $createCustomNode, CustomNode } from "./custom-node"; | ||
import { $createTemplateNode, TemplateNode } from "./template-node"; | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
import { useEffect, useCallback, useMemo } from "react"; | ||
import { useEffect, useCallback } from "react"; | ||
import { useLexicalTextEntity } from "@lexical/react/useLexicalTextEntity"; | ||
|
||
const escapeRegExp = (string: string) => { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
}; | ||
|
||
export const TemplateAutoReplacePlugin = ({ | ||
inputs, | ||
}: { | ||
inputs: string[]; | ||
}): JSX.Element | null => { | ||
export const TemplateAutoReplacePlugin = ( | ||
{ | ||
// inputs, | ||
}: { | ||
// inputs: string[]; | ||
} | ||
): JSX.Element | null => { | ||
const [editor] = useLexicalComposerContext(); | ||
const inputs: string[] = ["genre"]; | ||
useEffect(() => { | ||
if (!editor.hasNodes([CustomNode])) { | ||
if (!editor.hasNodes([TemplateNode])) { | ||
throw new Error( | ||
"TemplateAutoReplacePlugin: CustomNode not registered on editor" | ||
"TemplateAutoReplacePlugin: TemplateNode not registered on editor" | ||
); | ||
} | ||
}, [editor]); | ||
|
||
const $createCustomNode_ = useCallback((textNode: TextNode): CustomNode => { | ||
return $createCustomNode(textNode.getTextContent()); | ||
const createTemplateNode = useCallback((textNode: TextNode): TemplateNode => { | ||
return $createTemplateNode(textNode.getTextContent()); | ||
}, []); | ||
|
||
const contentPattern = inputs.map(escapeRegExp).join("|"); | ||
|
||
const getTemplateMatch = useCallback((text: string) => { | ||
const matchArr = new RegExp( | ||
`(?<!\\{)\\{(${contentPattern})\\}(?!\\})`, | ||
"g" | ||
).exec(text); | ||
|
||
if (matchArr === null) { | ||
return null; | ||
} | ||
|
||
const matchedContent = matchArr[1]; | ||
const startOffset = matchArr.index; | ||
const endOffset = startOffset + matchedContent.length + 2; // +2 for the braces | ||
return { | ||
end: endOffset, | ||
start: startOffset, | ||
}; | ||
}, []); | ||
|
||
// useLexicalTextEntity<CustomNode>( | ||
// getTemplateMatch, | ||
// CustomNode, | ||
// $createCustomNode_ | ||
// ); | ||
console.log(inputs); | ||
const getTemplateMatch = useCallback( | ||
(text: string) => { | ||
console.log(inputs); | ||
const contentPattern = inputs.map(escapeRegExp).join("|"); | ||
const matchArr = new RegExp( | ||
`(?<!\\{)\\{(${contentPattern})\\}(?!\\})`, | ||
"g" | ||
).exec(text); | ||
|
||
if (matchArr === null) { | ||
return null; | ||
} | ||
|
||
const matchedContent = matchArr[1]; | ||
const startOffset = matchArr.index; | ||
const endOffset = startOffset + matchedContent.length + 2; // +2 for the braces | ||
return { | ||
end: endOffset, | ||
start: startOffset, | ||
}; | ||
}, | ||
[JSON.stringify(inputs)] | ||
); | ||
|
||
useLexicalTextEntity<TemplateNode>( | ||
getTemplateMatch, | ||
TemplateNode, | ||
createTemplateNode | ||
); | ||
|
||
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
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,67 @@ | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
import { TextNode } from "lexical"; | ||
import { useEffect } from "react"; | ||
import { $createTemplateNode, TemplateNode } from "./template-node"; | ||
|
||
function $findAndTransformTemplate( | ||
node: TextNode, | ||
inputs: string[] | ||
): null | TextNode { | ||
const text = node.getTextContent(); | ||
const regex = /\{(.*?)\}/g; | ||
let match; | ||
|
||
while ((match = regex.exec(text)) !== null) { | ||
const matchedContent = match[1]; // Content inside the braces | ||
const startOffset = match.index; | ||
const endOffset = startOffset + match[0].length; // Include braces in length | ||
if (inputs.includes(matchedContent)) { | ||
let targetNode; | ||
if (startOffset === 0) { | ||
[targetNode] = node.splitText(endOffset); | ||
} else { | ||
[, targetNode] = node.splitText(startOffset, endOffset); | ||
} | ||
|
||
const templateNode = $createTemplateNode(match[0]); | ||
targetNode.replace(templateNode); | ||
return templateNode; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function $textNodeTransform(node: TextNode, inputs: string[]): void { | ||
let targetNode: TextNode | null = node; | ||
|
||
while (targetNode !== null) { | ||
if (!targetNode.isSimpleText()) { | ||
return; | ||
} | ||
targetNode = $findAndTransformTemplate(targetNode, inputs); | ||
} | ||
} | ||
|
||
function useTemplates(editor, inputs: string[]): void { | ||
useEffect(() => { | ||
if (!editor.hasNodes([TemplateNode])) { | ||
throw new Error( | ||
"TemplateAutoReplacePlugin: TemplateNode not registered on editor" | ||
); | ||
} | ||
|
||
return editor.registerNodeTransform(TextNode, (node: TextNode) => { | ||
$textNodeTransform(node, inputs); | ||
}); | ||
}, [editor, inputs]); | ||
} | ||
|
||
export const TemplatePlugin = ({ | ||
inputs, | ||
}: { | ||
inputs: string[]; | ||
}): JSX.Element | null => { | ||
const [editor] = useLexicalComposerContext(); | ||
useTemplates(editor, inputs); | ||
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
Oops, something went wrong.