-
Notifications
You must be signed in to change notification settings - Fork 4
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
Open
sid597
wants to merge
2
commits into
main
Choose a base branch
from
eng-1549-convert-existing-page-to-node-roam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,10 @@ export type ModifyNodeDialogProps = { | |
| includeDefaultNodes?: boolean; // Include default nodes (Page, Block) in node type selector | ||
| imageUrl?: string; // For image conversion from canvas | ||
| disableNodeTypeChange?: boolean; // Disable node type selector (e.g. canvas contexts) | ||
| createOverride?: (args: { | ||
| formattedTitle: string; | ||
| configPageUid: string; | ||
| }) => Promise<string | void>; | ||
| onSuccess: (result: { | ||
| text: string; | ||
| uid: string; | ||
|
|
@@ -69,6 +73,7 @@ const ModifyNodeDialog = ({ | |
| includeDefaultNodes = false, | ||
| imageUrl, | ||
| disableNodeTypeChange = false, | ||
| createOverride, | ||
| onSuccess, | ||
| onClose, | ||
| }: RoamOverlayProps<ModifyNodeDialogProps>) => { | ||
|
|
@@ -416,13 +421,20 @@ const ModifyNodeDialog = ({ | |
| return; | ||
| } | ||
|
|
||
| // Create new discourse node | ||
| const newPageUid = await createDiscourseNode({ | ||
| text: formattedTitle, | ||
| configPageUid: selectedNodeType?.type ?? "", | ||
| extensionAPI, | ||
| imageUrl, | ||
| }); | ||
| const configPageUid = selectedNodeType?.type ?? ""; | ||
| const overrideUid = createOverride | ||
| ? await createOverride({ formattedTitle, configPageUid }) | ||
| : undefined; | ||
|
Member
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. Could we just do a single const overrideUid = createOverride
? await createOverride({ formattedTitle, configPageUid })
: await createDiscourseNode({
text: formattedTitle,
configPageUid,
extensionAPI,
imageUrl,
}); |
||
|
|
||
| const newPageUid = | ||
| overrideUid !== undefined | ||
| ? overrideUid | ||
| : await createDiscourseNode({ | ||
| text: formattedTitle, | ||
| configPageUid, | ||
| extensionAPI, | ||
| imageUrl, | ||
| }); | ||
|
|
||
| if (sourceBlockUid) { | ||
| const pageRef = `[[${formattedTitle}]]`; | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -19,6 +19,10 @@ import { | |
| getOverlayHandler, | ||
| onPageRefObserverChange, | ||
| } from "./pageRefObserverHandlers"; | ||
| import findDiscourseNode from "~/utils/findDiscourseNode"; | ||
| import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid"; | ||
| import getSubTree from "roamjs-components/util/getSubTree"; | ||
| import stripUid from "roamjs-components/util/stripUid"; | ||
| import { HIDE_METADATA_KEY } from "~/data/userSettings"; | ||
| import posthog from "posthog-js"; | ||
| import { extractRef } from "roamjs-components/util"; | ||
|
|
@@ -121,6 +125,95 @@ export const createDiscourseNodeFromCommand = ( | |
| }); | ||
| }; | ||
|
|
||
| export const convertPageToNodeFromCommand = ( | ||
| extensionAPI: OnloadArgs["extensionAPI"], | ||
| ) => { | ||
| posthog.capture("Discourse Node: Convert Command Triggered"); | ||
| const pageUid = getCurrentPageUid(); | ||
| if (!pageUid) { | ||
| renderToast({ | ||
| id: "convert-page-no-page", | ||
| content: "Navigate to a page to convert it to a discourse node.", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const pageTitle = getPageTitleByPageUid(pageUid); | ||
| if (!pageTitle) { | ||
| renderToast({ | ||
| id: "convert-page-no-title", | ||
| content: "Could not determine the current page title.", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const existingNode = findDiscourseNode({ uid: pageUid, title: pageTitle }); | ||
| if (existingNode && existingNode.backedBy !== "default") { | ||
| renderToast({ | ||
| id: "convert-page-already-node", | ||
| content: `This page is already a ${existingNode.text} node.`, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| renderModifyNodeDialog({ | ||
| mode: "create", | ||
| nodeType: "", | ||
| initialValue: { text: pageTitle, uid: "" }, | ||
| extensionAPI, | ||
| createOverride: async ({ formattedTitle, configPageUid }) => { | ||
| await window.roamAlphaAPI.data.page.update({ | ||
| page: { uid: pageUid, title: formattedTitle }, | ||
| }); | ||
|
|
||
| const nodeTree = getFullTreeByParentUid(configPageUid).children; | ||
| const templateNode = getSubTree({ tree: nodeTree, key: "template" }); | ||
| if (templateNode.children.length > 0) { | ||
| const hasSmartBlockSyntax = ( | ||
| node: Parameters<typeof stripUid>[0][0], | ||
| ): boolean => { | ||
| if (node.text.includes("<%")) return true; | ||
| if (node.children) return node.children.some(hasSmartBlockSyntax); | ||
| return false; | ||
| }; | ||
| const useSmartBlocks = hasSmartBlockSyntax(templateNode); | ||
|
|
||
| if (useSmartBlocks && window.roamjs?.extension?.smartblocks) { | ||
| void window.roamjs.extension.smartblocks?.triggerSmartblock({ | ||
| srcUid: templateNode.uid, | ||
| targetUid: pageUid, | ||
| }); | ||
| } else { | ||
| if (useSmartBlocks) { | ||
| renderToast({ | ||
| content: | ||
| "This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.", | ||
| id: "smartblocks-extension-disabled", | ||
| intent: "warning", | ||
| }); | ||
| } | ||
| const existingChildren = | ||
| getFullTreeByParentUid(pageUid).children || []; | ||
| const lastOrder = existingChildren.length; | ||
| await Promise.all( | ||
| stripUid(templateNode.children).map((node, order) => | ||
| createBlock({ | ||
| node, | ||
| order: lastOrder + order, | ||
| parentUid: pageUid, | ||
| }), | ||
| ), | ||
|
sid597 marked this conversation as resolved.
Comment on lines
+169
to
+205
Member
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. This looks like code that is used elsewhere. Can we DRY it? |
||
| ); | ||
| } | ||
| } | ||
|
|
||
| return pageUid; | ||
| }, | ||
| onSuccess: async () => {}, | ||
| onClose: () => {}, | ||
| }); | ||
| }; | ||
|
|
||
| export const registerCommandPaletteCommands = (onloadArgs: OnloadArgs) => { | ||
| const { extensionAPI } = onloadArgs; | ||
|
|
||
|
|
@@ -314,6 +407,9 @@ export const registerCommandPaletteCommands = (onloadArgs: OnloadArgs) => { | |
| }; | ||
|
|
||
| // Roam organizes commands alphabetically | ||
| void addCommand("DG: Convert current page to discourse node", () => | ||
| convertPageToNodeFromCommand(extensionAPI), | ||
| ); | ||
| void addCommand("DG: Create/Insert discourse node", () => | ||
| createDiscourseNodeFromCommand(extensionAPI), | ||
| ); | ||
|
|
||
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.
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.
Why
void?