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
10 changes: 2 additions & 8 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ import { getMarkAttributes, isActive } from '@tiptap/core'
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
import NcActionInput from '@nextcloud/vue/components/NcActionInput'
import NcActions from '@nextcloud/vue/components/NcActions'
import { getLinkWithPicker } from '@nextcloud/vue/components/NcRichText'
import { useFileProps } from '../../composables/useFileProps.ts'
import { useLinkFile } from '../../composables/useLinkFile.ts'
import { useNetworkState } from '../../composables/useNetworkState.ts'
Expand Down Expand Up @@ -229,13 +228,8 @@ export default {
},

linkPicker() {
getLinkWithPicker(null, true)
.then((link) => {
this.insertLink(link)
})
.catch((error) => {
logger.error('Smart picker promise rejected', error)
})
this.menuOpen = false
this.editor?.chain().focus().openLinkPicker().run()
},

linkCustomAction() {
Expand Down
1 change: 1 addition & 0 deletions src/components/Suggestion/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default ({
}) => ({
items,
command,
decorationClass: 'suggestion-query',
render: () => {
let component
let popup
Expand Down
17 changes: 2 additions & 15 deletions src/components/SuggestionsBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@
<script>
import { t } from '@nextcloud/l10n'
import NcButton from '@nextcloud/vue/components/NcButton'
import { getLinkWithPicker } from '@nextcloud/vue/components/NcRichText'
import { Folder, Shape, Table as TableIcon, Upload } from '../components/icons.js'
import { useConnection } from '../composables/useConnection.ts'
import { useEditor } from '../composables/useEditor.ts'
import { useFileProps } from '../composables/useFileProps.ts'
import { useLinkFile } from '../composables/useLinkFile.ts'
import { useNetworkState } from '../composables/useNetworkState.ts'
import { isMobileDevice } from '../helpers/isMobileDevice.js'
import { logger } from '../helpers/logger.ts'
import { useActionChooseLocalAttachmentMixin } from './Editor/MediaHandler.provider.js'

export default {
Expand Down Expand Up @@ -132,22 +130,11 @@ export default {

methods: {
/**
* Open smart picker dialog
* Open smart picker inline menu
* Triggered by the "Smart Picker" button
*/
linkPicker() {
getLinkWithPicker(null, true)
.then((link) => {
const chain = this.editor.chain()
if (this.editor.view.state.selection.empty) {
chain.focus().insertPreview(link).run()
} else {
chain.setLink({ href: link }).focus().run()
}
})
.catch((error) => {
logger.error('Smart picker promise rejected', error)
})
this.editor.chain().focus().openLinkPicker().run()
},

/**
Expand Down
7 changes: 7 additions & 0 deletions src/css/prosemirror.scss
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ div.ProseMirror {
}
}

// Query of an open suggestion menu, styled like an inline input without shifting the text
.suggestion-query {
border-radius: var(--border-radius-small);
background-color: var(--color-primary-element-light);
padding-block: 2px;
}

.footnote-highlight {
animation: highlight-animation 5s 1;
border-radius: var(--border-radius-small);
Expand Down
47 changes: 44 additions & 3 deletions src/extensions/LinkPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

import { Extension } from '@tiptap/core'
import { PluginKey } from '@tiptap/pm/state'
import { Suggestion } from '@tiptap/suggestion'
import { PluginKey, TextSelection } from '@tiptap/pm/state'
import { findSuggestionMatch, Suggestion } from '@tiptap/suggestion'
import suggestions from '../components/Suggestion/LinkPicker/suggestions.js'

export const LinkPickerPluginKey = new PluginKey('linkPicker')
Expand All @@ -16,17 +16,58 @@ export default Extension.create({
return {
suggestion: {
char: '/',
allowedPrefixes: [' '],
// Typed trigger characters need to follow whitespace, see `shouldShow`
allowedPrefixes: null,
pluginKey: LinkPickerPluginKey,
allow: ({ state, range }) => {
const $from = state.doc.resolve(range.from)
return $from.parent.type.name !== 'codeBlock'
},
shouldShow: ({ editor, range, text, transaction }) => {
const typed = findSuggestionMatch({
char: '/',
allowSpaces: false,
allowToIncludeChar: false,
allowedPrefixes: [' '],
startOfLine: false,
$position: transaction.selection.$from,
})
if (typed?.range.from === range.from || transaction.getMeta(LinkPickerPluginKey)?.open) {
return true
}
// Keep a link picker opened by `openLinkPicker` open. `editor.state` is the state before `transaction`.
// Remote changes replace the whole document, so also compare the text.
const prev = LinkPickerPluginKey.getState(editor.state)
return prev.active
&& (transaction.mapping.map(prev.range.from) === range.from || text === prev.text)
},
...suggestions(),
},
}
},

addCommands() {
return {
/**
* Insert the trigger character after the cursor or selection and open the link picker,
* also within words.
*/
openLinkPicker: () => ({ editor, state, tr, dispatch }) => {
const { char, allow } = this.options.suggestion
const { to } = tr.selection
if (!allow({ editor, state, range: { from: to, to } })) {
return false
}
if (dispatch) {
tr.insertText(char, to)
.setSelection(TextSelection.create(tr.doc, to + char.length))
.setMeta(LinkPickerPluginKey, { open: true })
}
return true
},
}
},

addProseMirrorPlugins() {
return [
Suggestion({
Expand Down
160 changes: 160 additions & 0 deletions src/tests/extensions/LinkPicker.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { CodeBlock } from '@tiptap/extension-code-block'
import { TextSelection } from '@tiptap/pm/state'
import { exitSuggestion } from '@tiptap/suggestion'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import LinkPicker, { LinkPickerPluginKey } from '../../extensions/LinkPicker.js'
import createCustomEditor from '../testHelpers/createCustomEditor.ts'

describe('LinkPicker extension', () => {
let editor
let suggestionProps

const pickerState = () => LinkPickerPluginKey.getState(editor.state)
const queryDecorations = () => [...editor.view.dom.querySelectorAll('.suggestion-query')]
.map((el) => el.textContent)
const pickInsertAction = async (content) => {
await vi.waitFor(() => expect(suggestionProps).toBeDefined())
suggestionProps.command({ action: (chain) => chain.insertContent(content) })
}

beforeEach(() => {
suggestionProps = undefined
const render = () => ({
onStart: (props) => { suggestionProps = props },
onUpdate: (props) => { suggestionProps = props },
})
editor = createCustomEditor('<p>Hello world</p>', [
CodeBlock,
LinkPicker.configure({
suggestion: { items: () => [], render },
}),
])
})

afterEach(() => {
editor.destroy()
})

describe('typing the trigger character', () => {
it('opens after whitespace', () => {
editor.chain().setTextSelection(7).insertContent('/').run()
expect(pickerState()).toMatchObject({ active: true, range: { from: 7, to: 8 } })
})

it('does not open within a word', () => {
editor.chain().setTextSelection(4).insertContent('/').run()
expect(pickerState().active).toBe(false)
})
})

describe('openLinkPicker command', () => {
it('inserts the trigger character and opens within a word', () => {
editor.chain().setTextSelection(4).openLinkPicker().run()
expect(editor.state.doc.textContent).toBe('Hel/lo world')
expect(editor.state.selection.from).toBe(5)
expect(pickerState()).toMatchObject({
active: true,
range: { from: 4, to: 5 },
query: '',
text: '/',
})
})

it('uses text typed after the trigger character as query', () => {
editor.chain().setTextSelection(4).openLinkPicker().run()
editor.commands.insertContent('ab')
expect(pickerState()).toMatchObject({
active: true,
range: { from: 4, to: 7 },
query: 'ab',
})
})

it('styles the query while open', () => {
editor.chain().setTextSelection(4).openLinkPicker().run()
editor.commands.insertContent('ab')
expect(queryDecorations()).toEqual(['/ab'])
})

it('inserts the trigger character after selected text', () => {
editor.chain().setTextSelection({ from: 1, to: 6 }).openLinkPicker().run()
expect(editor.state.doc.textContent).toBe('Hello/ world')
expect(pickerState()).toMatchObject({ active: true, range: { from: 6, to: 7 } })
})

it('replaces the trigger character and query with the picked entry', async () => {
editor.chain().setTextSelection({ from: 1, to: 6 }).openLinkPicker().run()
editor.commands.insertContent('ab')
await pickInsertAction('X')
expect(editor.state.doc.textContent).toBe('HelloX world')
expect(pickerState().active).toBe(false)
})

it('does not open in a code block', () => {
editor.commands.setContent('<pre><code>code</code></pre>')
expect(editor.chain().setTextSelection(3).openLinkPicker().run()).toBe(false)
expect(editor.state.doc.textContent).toBe('code')
})

it('does not open in a non-editable editor', () => {
editor.setEditable(false)
editor.chain().setTextSelection(4).openLinkPicker().run()
expect(pickerState().active).toBe(false)
})
})

describe('closing', () => {
beforeEach(() => {
editor.chain().setTextSelection(4).openLinkPicker().run()
})

it('closes on whitespace', () => {
editor.commands.insertContent(' ')
expect(pickerState().active).toBe(false)
})

it('closes when the cursor moves before the trigger character', () => {
editor.commands.setTextSelection(4)
expect(pickerState().active).toBe(false)
})

it('closes when the trigger character is deleted', () => {
editor.commands.deleteRange({ from: 4, to: 5 })
expect(pickerState().active).toBe(false)
})

it('keeps the text and removes the styling on exit', () => {
editor.commands.insertContent('ab')
exitSuggestion(editor.view, LinkPickerPluginKey)
expect(pickerState().active).toBe(false)
expect(editor.state.doc.textContent).toBe('Hel/ablo world')
expect(queryDecorations()).toEqual([])
})
})

it('does not reopen a dismissed trigger character after whitespace', () => {
editor.chain().setTextSelection(7).openLinkPicker().run()
exitSuggestion(editor.view, LinkPickerPluginKey)
editor.commands.insertContent('a')
expect(pickerState().active).toBe(false)
})

it('keeps the query when the whole document is replaced', () => {
editor.chain().setTextSelection(4).openLinkPicker().run()
editor.commands.insertContent('ab')
const { tr } = editor.state
tr.replaceWith(0, tr.doc.content.size, editor.state.doc.content)
tr.setSelection(TextSelection.create(tr.doc, 7))
editor.view.dispatch(tr)
expect(pickerState()).toMatchObject({
active: true,
range: { from: 4, to: 7 },
query: 'ab',
})
})
})
Loading