@@ -8,7 +8,7 @@ import NcReferencePickerModal from './../../components/NcRichText/NcReferencePic
88import { getProvider } from './providerHelper.js'
99
1010/**
11- * Creates a reference picker modal and return a promise which provides the result
11+ * Creates a reference picker modal and return a promise which provides the link URL
1212 *
1313 * @param providerId - Optional ID of initial selected provider
1414 * @param isInsideViewer - Should be true if this function is called while the Viewer is displayed
@@ -37,3 +37,40 @@ export async function getLinkWithPicker(providerId?: string, isInsideViewer?: bo
3737
3838 return promise
3939}
40+
41+ /**
42+ * Creates a reference picker modal and return a promise which provides the reference object.
43+ *
44+ * The function guarantees a `link: string` on the resolved object. Custom picker elements may
45+ * include additional fields (e.g. `title`).
46+ *
47+ * @experimental The shape of the resolved reference and the signature of this function may still change.
48+ *
49+ * @template T - Expected shape of the resolved reference, must include `link: string`
50+ * @param providerId - Optional ID of initial selected provider
51+ * @param isInsideViewer - Should be true if this function is called while the Viewer is displayed
52+ */
53+ export async function getReferenceWithPicker < T extends { link : string } > ( providerId ?: string , isInsideViewer ?: boolean ) : Promise < T > {
54+ const modalId = 'referencePickerModal'
55+ const modalElement = document . createElement ( 'div' )
56+ modalElement . id = modalId
57+ document . body . append ( modalElement )
58+
59+ const { promise, reject, resolve } = Promise . withResolvers < T > ( )
60+ const initialProvider = ( providerId && getProvider ( providerId ) ) || null
61+ const view = createApp ( NcReferencePickerModal , {
62+ initialProvider,
63+ isInsideViewer,
64+ onCancel ( ) {
65+ view . unmount ( )
66+ reject ( new Error ( 'User cancellation' ) )
67+ } ,
68+ onPick ( reference : T ) {
69+ view . unmount ( )
70+ resolve ( reference )
71+ } ,
72+ } )
73+ view . mount ( modalElement )
74+
75+ return promise
76+ }
0 commit comments