Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SmartPicker): Insert smart picker links as preview per default #5846

Merged
merged 1 commit into from
Jun 18, 2024
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 @@ -129,6 +129,21 @@

})

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)
mejo- marked this conversation as resolved.
Show resolved Hide resolved
expectPreview()
})

})

/**
* Expect a preview in the editor.
*/
Expand Down Expand Up @@ -157,7 +172,7 @@

/**
*
* @param input

Check warning on line 175 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "input" description

Check warning on line 175 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "input" type
*/
function prepareEditor(input) {
loadMarkdown(editor, input)
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: [{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit unexpected that the preview node still has an additional mark, can you tell why?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it matches the data structure created otherwise. We basically change the wrapping block node between paragraph and preview when switching between the two:

The setPreview function above converts the surrounding paragraph of a link into a preview node:
grafik

If you look at unsetPreview above it also only unwraps the link inside the preview node:
grafik

type: 'link',
attrs: { href: link },
}],
text: link,
}],
})
.run()
},
}
},
})
Expand Down
Loading