-
Notifications
You must be signed in to change notification settings - Fork 7
ENG-1893 Open node creation dialog for highlighted text #1296
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
base: main
Are you sure you want to change the base?
Changes from all commits
b309a81
a234e7b
0e03785
30c10a8
d302c2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import type { OnloadArgs } from "roamjs-components/types"; | ||
| import type { ModifyNodeDialogProps } from "~/components/ModifyNodeDialog"; | ||
|
|
||
| type InsertPageRefAtRangeArgs = { | ||
| blockUid: string; | ||
| pageTitle: string; | ||
| selectionEnd: number; | ||
| selectionStart: number; | ||
| windowId: string; | ||
| }; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| insertPageRefAtRange: | ||
| vi.fn<(args: InsertPageRefAtRangeArgs) => Promise<void>>(), | ||
| renderModifyNodeDialog: vi.fn<(props: ModifyNodeDialogProps) => void>(), | ||
| })); | ||
|
|
||
| vi.mock("~/components/ModifyNodeDialog", () => ({ | ||
| renderModifyNodeDialog: mocks.renderModifyNodeDialog, | ||
| })); | ||
|
|
||
| vi.mock("~/utils/advancedSearchFooterUtils", () => ({ | ||
| insertPageRefAtRange: mocks.insertPageRefAtRange, | ||
| })); | ||
|
|
||
| import { openCreateNodeDialogFromSelection } from "~/utils/openCreateNodeDialogFromSelection"; | ||
|
|
||
| describe("openCreateNodeDialogFromSelection", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.insertPageRefAtRange.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it("prefills the dialog and replaces the selected text after creation", async () => { | ||
| const extensionAPI = {} as OnloadArgs["extensionAPI"]; | ||
| const onInserted = vi.fn(); | ||
|
|
||
| openCreateNodeDialogFromSelection({ | ||
| blockUid: "block-uid", | ||
| extensionAPI, | ||
| nodeType: "node-type-uid", | ||
| onInserted, | ||
| selectedText: "highlighted text", | ||
| selectionEnd: 20, | ||
| selectionStart: 4, | ||
| windowId: "main-window", | ||
| }); | ||
|
|
||
| expect(mocks.renderModifyNodeDialog).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| extensionAPI, | ||
| initialValue: { text: "highlighted text", uid: "" }, | ||
| mode: "create", | ||
| nodeType: "node-type-uid", | ||
| }), | ||
| ); | ||
|
|
||
| const dialogProps = mocks.renderModifyNodeDialog.mock.calls[0][0]; | ||
| await dialogProps.onSuccess({ | ||
| action: "create", | ||
| text: "CLM - highlighted text", | ||
| uid: "new-node-uid", | ||
| }); | ||
|
|
||
| expect(mocks.insertPageRefAtRange).toHaveBeenCalledWith({ | ||
| blockUid: "block-uid", | ||
| pageTitle: "CLM - highlighted text", | ||
| selectionEnd: 20, | ||
| selectionStart: 4, | ||
| windowId: "main-window", | ||
| }); | ||
| expect(onInserted).toHaveBeenCalledWith("CLM - highlighted text"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { renderModifyNodeDialog } from "~/components/ModifyNodeDialog"; | ||
| import { insertPageRefAtRange } from "~/utils/advancedSearchFooterUtils"; | ||
| import type { OnloadArgs } from "roamjs-components/types"; | ||
|
|
||
| type OpenCreateNodeDialogFromSelectionArgs = { | ||
| blockUid: string; | ||
| extensionAPI: OnloadArgs["extensionAPI"]; | ||
| nodeType: string; | ||
| onInserted?: (pageTitle: string) => void; | ||
| selectedText: string; | ||
| selectionEnd: number; | ||
| selectionStart: number; | ||
| windowId: string; | ||
| }; | ||
|
|
||
| export const openCreateNodeDialogFromSelection = ({ | ||
| blockUid, | ||
| extensionAPI, | ||
| nodeType, | ||
| onInserted, | ||
| selectedText, | ||
| selectionEnd, | ||
| selectionStart, | ||
| windowId, | ||
| }: OpenCreateNodeDialogFromSelectionArgs): void => { | ||
| renderModifyNodeDialog({ | ||
| mode: "create", | ||
| nodeType, | ||
| initialValue: { text: selectedText, uid: "" }, | ||
| extensionAPI, | ||
|
mdroidian marked this conversation as resolved.
Comment on lines
+29
to
+30
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.
When the selected node type's format contains a referenced-node placeholder, confirming the prefilled dialog without manually choosing that reference calls Useful? React with 👍 / 👎. |
||
| onSuccess: async (result) => { | ||
| await insertPageRefAtRange({ | ||
| blockUid, | ||
| pageTitle: result.text, | ||
| selectionEnd, | ||
| selectionStart, | ||
|
mdroidian marked this conversation as resolved.
|
||
| windowId, | ||
| }); | ||
| onInserted?.(result.text); | ||
| }, | ||
| onClose: () => {}, | ||
| }); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.