Skip to content

Commit

Permalink
Merge pull request #5846 from nextcloud/fix/smart_picker_preview
Browse files Browse the repository at this point in the history
fix(SmartPicker): Insert smart picker links as preview per default
  • Loading branch information
mejo- authored Jun 18, 2024
2 parents 94beb0c + bc5f65c commit 00cc04e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
15 changes: 15 additions & 0 deletions cypress/e2e/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ describe('Preview extension', { retries: 0 }, () => {

})

describe('insertPreview command', { retries: 0 }, () => {

it('is available in commands', () => {
expect(editor.commands).to.have.property('insertPreview')
})

it('inserts a preview', () => {
editor.commands.clearContent()
editor.commands.insertPreview('https://nextcloud.com')
editor.commands.setTextSelection(1)
expectPreview()
})

})

/**
* Expect a preview in the editor.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export default {
.then(link => {
const chain = this.$editor.chain()
if (this.$editor.view.state?.selection.empty) {
chain.focus().insertContent(link + ' ').run()
chain.focus().insertPreview(link).run()
} else {
chain.setLink({ href: link }).focus().run()
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Suggestion/LinkPicker/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export default () => createSuggestions({
editor
.chain()
.focus()
.insertContentAt(range, content + ' ')
.deleteRange(range)
.insertPreview(link)
.run()
})
.catch(error => {
Expand Down
22 changes: 21 additions & 1 deletion src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default Node.create({
state.write('[')
state.text(node.textContent, false)
state.write(`](${node.attrs.href} (${node.attrs.title}))`)
state.closeBlock(node)
},

addCommands() {
Expand All @@ -84,13 +85,32 @@ export default Node.create({
*
*/
unsetPreview: () => ({ state, chain }) => {
console.info(this.attributes)
return isActive(this.name, this.attributes, state)
&& chain()
.setNode('paragraph')
.run()
},

/**
* Insert a preview for given link.
*
*/
insertPreview: (link) => ({ state, chain }) => {
return chain()
.insertContent({
type: 'preview',
attrs: { href: link, title: 'preview' },
content: [{
type: 'text',
marks: [{
type: 'link',
attrs: { href: link },
}],
text: link,
}],
})
.run()
},
}
},
})
Expand Down

0 comments on commit 00cc04e

Please sign in to comment.