Skip to content
Merged
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
15 changes: 15 additions & 0 deletions cypress/e2e/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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 @@ -233,7 +233,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 @@ -102,7 +102,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 @@ -79,6 +79,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 @@ -101,13 +102,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