-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1549: Add convert existing page to discourse node command #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a8875db
ENG-1549 Add convert existing page to discourse node command
sid597 26330ad
Add SmartBlocks support and fix unused uid destructuring
sid597 81b0227
Extract insertTemplateBlocks helper and tighten createOverride type
sid597 44ebe15
Fold image creation into insertTemplateBlocks helper
sid597 6f37b49
Restore handleImageCreation body unchanged at module scope
sid597 d79a736
Simplify: just extract helpers, drop the wrapper
sid597 d5deabb
Fix shared template creation flow
sid597 bc2ffcd
Fix template helper lint warnings
sid597 993a866
Append converted page templates after existing blocks
sid597 b473b2c
Merge remote-tracking branch 'origin/main' into eng-1549-convert-exis…
sid597 16a07de
Merge remote-tracking branch 'origin/main' into eng-1549-convert-exis…
sid597 2e43327
Restore eslint-disable for NODETEXT/NODEUID input keys
sid597 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -40,6 +40,147 @@ const stripTemplateUids = (nodes: InputTextNode[]): InputTextNode[] => | |
| }; | ||
| }); | ||
|
|
||
| const handleImageCreation = async ({ | ||
| pageUid, | ||
| discourseNodes, | ||
| configPageUid, | ||
| imageUrl, | ||
| extensionAPI, | ||
| text, | ||
| }: { | ||
| pageUid: string; | ||
| discourseNodes: ReturnType<typeof getDiscourseNodes>; | ||
| configPageUid: string; | ||
| imageUrl?: string; | ||
| extensionAPI?: OnloadArgs["extensionAPI"]; | ||
| text: string; | ||
| }) => { | ||
| const canvasSettings = Object.fromEntries( | ||
| discourseNodes.map((n) => [n.type, { ...n.canvasSettings }]), | ||
| ); | ||
| const { | ||
| "query-builder-alias": qbAlias = "", | ||
| "key-image": isKeyImage = "", | ||
| "key-image-option": keyImageOption = "", | ||
| } = canvasSettings[configPageUid] || {}; | ||
|
|
||
| if (isKeyImage && imageUrl) { | ||
| const createOrUpdateImageBlock = async (imagePlaceholderUid?: string) => { | ||
| const imageMarkdown = ``; | ||
| if (imagePlaceholderUid) { | ||
| await updateBlock({ | ||
| uid: imagePlaceholderUid, | ||
| text: imageMarkdown, | ||
| }); | ||
| } else { | ||
| await createBlock({ | ||
| node: { text: imageMarkdown }, | ||
| order: 0, | ||
| parentUid: pageUid, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| if (keyImageOption === "query-builder") { | ||
| if (!extensionAPI) return; | ||
|
|
||
| const parentUid = resolveQueryBuilderRef({ queryRef: qbAlias }); | ||
| const results = await runQuery({ | ||
| extensionAPI, | ||
| parentUid, | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| inputs: { NODETEXT: text, NODEUID: pageUid }, | ||
| }); | ||
| const imagePlaceholderUid = results.allProcessedResults[0]?.uid; | ||
| await createOrUpdateImageBlock(imagePlaceholderUid); | ||
| } else { | ||
| await createOrUpdateImageBlock(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const createBlocksFromTemplate = async ({ | ||
| templateChildren, | ||
| pageUid, | ||
| order = 0, | ||
| discourseNodes, | ||
| configPageUid, | ||
| imageUrl, | ||
| extensionAPI, | ||
| text, | ||
| }: { | ||
| templateChildren: InputTextNode[]; | ||
| pageUid: string; | ||
| order?: number; | ||
| discourseNodes: ReturnType<typeof getDiscourseNodes>; | ||
| configPageUid: string; | ||
| imageUrl?: string; | ||
| extensionAPI?: OnloadArgs["extensionAPI"]; | ||
| text: string; | ||
| }) => { | ||
| const createBlocks = async () => { | ||
| await Promise.all( | ||
| stripTemplateUids(templateChildren).map((node, templateOrder) => | ||
| createBlock({ | ||
| node, | ||
| order: order + templateOrder, | ||
| parentUid: pageUid, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| await handleImageCreation({ | ||
| pageUid, | ||
| discourseNodes, | ||
| configPageUid, | ||
| imageUrl, | ||
| extensionAPI, | ||
| text, | ||
| }); | ||
| }; | ||
|
|
||
| const hasSmartBlockSyntax = (node: InputTextNode): boolean => { | ||
| if (node.text.includes("<%")) return true; | ||
| if (node.children) return node.children.some(hasSmartBlockSyntax); | ||
| return false; | ||
| }; | ||
| const useSmartBlocks = templateChildren.some(hasSmartBlockSyntax); | ||
| const legacyTemplateNode = getSubTree({ | ||
| tree: getFullTreeByParentUid(configPageUid).children, | ||
| key: "template", | ||
| }); | ||
| const canUseLegacySmartBlock = !!legacyTemplateNode.uid; | ||
|
|
||
| if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) { | ||
| renderToast({ | ||
| content: | ||
| "This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.", | ||
| id: "smartblocks-extension-disabled", | ||
| intent: "warning", | ||
| }); | ||
| await createBlocks(); | ||
| } else if ( | ||
| useSmartBlocks && | ||
| canUseLegacySmartBlock && | ||
| window.roamjs?.extension?.smartblocks | ||
| ) { | ||
| void window.roamjs.extension.smartblocks?.triggerSmartblock({ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| srcUid: legacyTemplateNode.uid, | ||
| targetUid: pageUid, | ||
| }); | ||
| await handleImageCreation({ | ||
| pageUid, | ||
| discourseNodes, | ||
| configPageUid, | ||
| imageUrl, | ||
| extensionAPI, | ||
| text, | ||
| }); | ||
| } else { | ||
| await createBlocks(); | ||
| } | ||
| }; | ||
|
|
||
| const createDiscourseNode = async ({ | ||
| text, | ||
| configPageUid, | ||
|
|
@@ -69,49 +210,6 @@ const createDiscourseNode = async ({ | |
| }, 1); | ||
| }, 100); | ||
| }; | ||
| const handleImageCreation = async (pageUid: string) => { | ||
| const canvasSettings = Object.fromEntries( | ||
| discourseNodes.map((n) => [n.type, { ...n.canvasSettings }]), | ||
| ); | ||
| const { | ||
| "query-builder-alias": qbAlias = "", | ||
| "key-image": isKeyImage = "", | ||
| "key-image-option": keyImageOption = "", | ||
| } = canvasSettings[configPageUid] || {}; | ||
|
|
||
| if (isKeyImage && imageUrl) { | ||
| const createOrUpdateImageBlock = async (imagePlaceholderUid?: string) => { | ||
| const imageMarkdown = ``; | ||
| if (imagePlaceholderUid) { | ||
| await updateBlock({ | ||
| uid: imagePlaceholderUid, | ||
| text: imageMarkdown, | ||
| }); | ||
| } else { | ||
| await createBlock({ | ||
| node: { text: imageMarkdown }, | ||
| order: 0, | ||
| parentUid: pageUid, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| if (keyImageOption === "query-builder") { | ||
| if (!extensionAPI) return; | ||
|
|
||
| const parentUid = resolveQueryBuilderRef({ queryRef: qbAlias }); | ||
| const results = await runQuery({ | ||
| extensionAPI, | ||
| parentUid, | ||
| inputs: { NODETEXT: text, NODEUID: pageUid }, | ||
| }); | ||
| const imagePlaceholderUid = results.allProcessedResults[0]?.uid; | ||
| await createOrUpdateImageBlock(imagePlaceholderUid); | ||
| } else { | ||
| await createOrUpdateImageBlock(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const discourseNodes = getDiscourseNodes(); | ||
| const specification = discourseNodes?.find( | ||
|
|
@@ -163,54 +261,15 @@ const createDiscourseNode = async ({ | |
| DISCOURSE_NODE_KEYS.template, | ||
| ]) ?? []; | ||
|
|
||
| const createBlocksFromTemplate = async () => { | ||
| await Promise.all( | ||
| stripTemplateUids(templateChildren).map((node, order) => | ||
| createBlock({ | ||
| node, | ||
| order, | ||
| parentUid: pageUid, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| // Add image to page if imageUrl is provided | ||
| await handleImageCreation(pageUid); | ||
| }; | ||
|
|
||
| const hasSmartBlockSyntax = (node: InputTextNode): boolean => { | ||
| if (node.text.includes("<%")) return true; | ||
| if (node.children) return node.children.some(hasSmartBlockSyntax); | ||
| return false; | ||
| }; | ||
| const useSmartBlocks = templateChildren.some(hasSmartBlockSyntax); | ||
| const legacyTemplateNode = getSubTree({ | ||
| tree: getFullTreeByParentUid(configPageUid).children, | ||
| key: "template", | ||
| await createBlocksFromTemplate({ | ||
| templateChildren, | ||
| pageUid, | ||
| discourseNodes, | ||
| configPageUid, | ||
| imageUrl, | ||
| extensionAPI, | ||
| text, | ||
| }); | ||
| const canUseLegacySmartBlock = !!legacyTemplateNode.uid; | ||
|
|
||
| if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) { | ||
| renderToast({ | ||
| content: | ||
| "This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.", | ||
| id: "smartblocks-extension-disabled", | ||
| intent: "warning", | ||
| }); | ||
| await createBlocksFromTemplate(); | ||
| } else if ( | ||
| useSmartBlocks && | ||
| canUseLegacySmartBlock && | ||
| window.roamjs?.extension?.smartblocks | ||
| ) { | ||
| void window.roamjs.extension.smartblocks?.triggerSmartblock({ | ||
| srcUid: legacyTemplateNode.uid, | ||
| targetUid: pageUid, | ||
| }); | ||
| await handleImageCreation(pageUid); | ||
| } else { | ||
| await createBlocksFromTemplate(); | ||
| } | ||
| handleOpenInSidebar(pageUid); | ||
| return pageUid; | ||
| }; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled out function from line 53, nothing changed in the implementation of this.