Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export default {
this.renderResult.object.$on('cancel', this.onCancel)
}
this.renderResult.element.addEventListener('submit', (e) => {
this.onSubmit(e.detail)
const detail = e.detail
const result = typeof detail === 'string' ? { link: detail } : detail
this.onSubmit(result)
})
this.renderResult.element.addEventListener('cancel', this.onCancel)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default {
onProviderSelected(p) {
if (p !== null) {
if (p.isLink) {
this.$emit('submit', p.title)
this.$emit('submit', { link: p.title })
} else {
this.$emit('selectProvider', p)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default {
onSubmit(e) {
const value = e.target.value
if (this.isLinkValid) {
this.$emit('submit', value)
this.$emit('submit', { link: value })
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ export default {
this.$emit('cancel')
},

submitLink(link) {
submitLink(result) {
if (this.selectedProvider !== null) {
touchProvider(this.selectedProvider.id)
}
this.$emit('submit', link)
this.$emit('submit', result)
this.deselectProvider()
},

Expand Down
3 changes: 2 additions & 1 deletion src/components/NcRichText/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
searchProvider,
sortProviders,
} from '../../functions/reference/providerHelper.ts'
import { getLinkWithPicker } from '../../functions/reference/referencePickerModal.ts'
import { getLinkWithPicker, getReferenceWithPicker } from '../../functions/reference/referencePickerModal.ts'
import { isWidgetRegistered, registerWidget, renderWidget } from './../../functions/reference/widgets.ts'

export default NcRichText
Expand All @@ -32,6 +32,7 @@ export {
getLinkWithPicker,
getProvider,
getProviders,
getReferenceWithPicker,
isCustomPickerElementRegistered,
isWidgetRegistered,
NcCustomPickerRenderResult,
Expand Down
5 changes: 4 additions & 1 deletion src/functions/reference/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export {
renderWidget,
} from './widgets.ts'

export { getLinkWithPicker } from './referencePickerModal.ts'
export {
getLinkWithPicker,
getReferenceWithPicker,
} from './referencePickerModal.ts'

export {
type ReferenceProvider,
Expand Down
42 changes: 39 additions & 3 deletions src/functions/reference/referencePickerModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { createApp } from 'vue'
import NcReferencePickerModal from './../../components/NcRichText/NcReferencePicker/NcReferencePickerModal.vue'
import { getProvider } from './providerHelper.js'

type PickerSubmitResult = {
link: string
title?: string
}

/**
* Creates a reference picker modal and return a promise which provides the result
* Creates a reference picker modal and return a promise which provides the link URL
*
* @param providerId - Optional ID of initial selected provider
* @param isInsideViewer - Should be true if this function is called while the Viewer is displayed
Expand All @@ -28,9 +33,40 @@ export async function getLinkWithPicker(providerId?: string, isInsideViewer?: bo
view.unmount()
reject(new Error('User cancellation'))
},
onSubmit(link: string) {
onSubmit(result: string | PickerSubmitResult) {
view.unmount()
resolve(typeof result === 'string' ? result : result.link)
},
})
view.mount(modalElement)

return promise
}

/**
* Creates a reference picker modal and return a promise which provides the reference object
*
* @param providerId - Optional ID of initial selected provider
* @param isInsideViewer - Should be true if this function is called while the Viewer is displayed
*/
export async function getReferenceWithPicker(providerId?: string, isInsideViewer?: boolean): Promise<PickerSubmitResult> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having isInsideViewer as a position parameter seems unexpected to me. Picker shouldn't know, it is it in Viewer. If it's really needed, I'd make it an option rather than a second position parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written in 1:1 chat, I kept it that way to keep getLinkWithPicker() and getReferenceWithPicker() as similar as possible to make it a no-brainer to migrate from one to the other.

const modalId = 'referencePickerModal'
const modalElement = document.createElement('div')
modalElement.id = modalId
document.body.append(modalElement)
Comment on lines +53 to +56
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have spawnDialog util to mount dialogs


const { promise, reject, resolve } = Promise.withResolvers<PickerSubmitResult>()
const initialProvider = (providerId && getProvider(providerId)) || null
const view = createApp(NcReferencePickerModal, {
initialProvider,
isInsideViewer,
onCancel() {
view.unmount()
reject(new Error('User cancellation'))
},
onSubmit(result: string | PickerSubmitResult) {
view.unmount()
resolve(link)
resolve(typeof result === 'string' ? { link: result } : result)
},
})
view.mount(modalElement)
Expand Down
Loading