Skip to content

Commit

Permalink
fix(SmartPicker): Show smart picker links as preview per default
Browse files Browse the repository at this point in the history
Fixes: #5838

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed May 29, 2024
1 parent d29e45a commit dbc28c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ export default {
getLinkWithPicker(null, true)
.then(link => {
const chain = this.$editor.chain()
if (this.$editor.view.state?.selection.empty) {
chain.focus().insertContent(link + ' ').run()
} else {
chain.setLink({ href: link }).focus().run()
}
chain.focus().insertPreview(link).run()
})
.catch(error => {
console.error('Smart picker promise rejected', error)
Expand Down
27 changes: 26 additions & 1 deletion src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default Node.create({
state.write('[')
state.text(node.textContent, false)
state.write(`](${node.attrs.href} (${node.attrs.title}))`)
state.write('\n')
},

addCommands() {
Expand All @@ -101,13 +102,37 @@ 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 }) => {
let linkText = link
if (!state.selection.empty) {
const { from, to } = state.selection
linkText = state.doc.textBetween(from, to)
}
return chain()
.insertContent({
type: 'preview',
attrs: { href: link, title: 'preview'},
content: [{
type: 'text',
marks: [{
type: 'link',
attrs: { href: link },
}],
text: linkText,
}],
})
.run()
},
}
},
})
Expand Down
15 changes: 15 additions & 0 deletions src/tests/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ describe('Preview extension', () => {
expect(node.attrs.href).toBe('https://nextcloud.com')
})

it('inserts preview', () => {
const editor = createEditorWithPreview()
editor.commands.insertPreview('https://nextcloud.com')
console.info(editor.getJSON().content[0].content)
let previewNode
editor.state.doc.content.descendants((node) => {
if (node.type === 'preview') {
previewNode = node
}
})
expect(previewNode.type.name).toBe('preview')
expect(previewNode.attrs.title).toBe('preview')
expect(previewNode.attrs.href).toBe('https://nextcloud.com')
})

})

function createEditorWithPreview() {
Expand Down

0 comments on commit dbc28c7

Please sign in to comment.