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 Jun 18, 2024
1 parent 0a6b3f6 commit f88e45e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 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
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 f88e45e

Please sign in to comment.