Skip to content

Commit

Permalink
enh(Preview): split togglePreview in setPreview and unsetPreview
Browse files Browse the repository at this point in the history
They actually need to do different things
due to the different structure.

On the one hand we have the preview node
with plain text content.
And on the other hand a paragraph node
with the text wrapped in a link mark.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Mar 5, 2024
1 parent 9e6b673 commit f3161a2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
63 changes: 50 additions & 13 deletions cypress/e2e/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,86 @@ describe.only('Preview extension', { retries: 0 }, () => {
],
})

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

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

it('cannot run on normal paragraph', () => {
prepareEditor('hello\n')
expect(editor.can().togglePreview()).to.be.false
expect(editor.can().setPreview()).to.be.false

Check failure on line 50 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('cannot run on a paragraph with a different mark', () => {
prepareEditor('*link text*\n')
expect(editor.can().togglePreview()).to.be.false
expect(editor.can().setPreview()).to.be.false

Check failure on line 55 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('cannot run on a paragraph with a link without a href', () => {
prepareEditor('[link text]()\n')
expect(editor.can().togglePreview()).to.be.false
expect(editor.can().setPreview()).to.be.false

Check failure on line 60 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('cannot run on a paragraph with an anchor link', () => {
prepareEditor('[link text](#top)\n')
expect(editor.can().togglePreview()).to.be.false
expect(editor.can().setPreview()).to.be.false

Check failure on line 65 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('cannot run on a paragraph with other content', () => {
prepareEditor('[link text](https://nextcloud.com) hello\n')
expect(editor.can().togglePreview()).to.be.false
expect(editor.can().setPreview()).to.be.false

Check failure on line 70 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('can run on a paragraph with a link', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
expect(editor.can().togglePreview()).to.be.true
expect(editor.can().setPreview()).to.be.true

Check failure on line 75 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('results in a preview node with the href', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.togglePreview()
editor.commands.setPreview()
expect(getParentNode().type.name).to.equal('preview')
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com')
})

it('reverts when toggled twice', () => {
it('cannot run twice', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.togglePreview()
editor.commands.togglePreview()
editor.commands.setPreview()
expect(editor.can().setPreview()).to.be.false

Check failure on line 88 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

})

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

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

it('cannot run on normal paragraph', () => {
prepareEditor('hello\n')
expect(editor.can().unsetPreview()).to.be.false

Check failure on line 101 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('can run on the output of setPreview', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.setPreview()
expect(editor.can().unsetPreview()).to.be.true

Check failure on line 107 in cypress/e2e/nodes/Preview.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected an assignment or function call and instead saw an expression
})

it('creates a paragraph', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.setPreview()
editor.commands.unsetPreview()
expect(getParentNode().type.name).to.equal('paragraph')
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com')
})

it('includes a link', () => {
prepareEditor('[link text](https://nextcloud.com)\n')
editor.commands.setPreview()
editor.commands.unsetPreview()
expect(getMark().attrs.href).to.equal('https://nextcloud.com')
})

})
Expand All @@ -97,6 +128,12 @@ describe.only('Preview extension', { retries: 0 }, () => {
return selection.$head.parent
}

function getMark() {

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

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc comment
const { state: { selection } } = editor
console.info(selection.$head)
return selection.$head.nodeAfter.marks[0]
}

function prepareEditor(input) {

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

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc comment
loadMarkdown(editor, input)
editor.commands.setTextSelection(1)
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ParagraphView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default {
this.$editor.chain()
.focus()
.setTextSelection(this.getPos())
.togglePreview({ href: this.href })
.setPreview({ href: this.href })
.run()
},
getTextReference(node) {
Expand Down
30 changes: 25 additions & 5 deletions src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

import { Node } from '@tiptap/core'
import { Node, isNodeActive, getNodeType } from '@tiptap/core'
import { domHref, parseHref } from './../helpers/links.js'
import { VueNodeViewRenderer } from '@tiptap/vue-2'

Expand Down Expand Up @@ -78,20 +78,35 @@ export default Node.create({

addCommands() {
return {

/**
* Turn a paragraph that contains a single link
* into a preview and vice versa.
* into a preview.
*
*/
togglePreview: () => ({ state, commands }) => {
setPreview: () => ({ state, commands }) => {
const { selection } = state
return previewPossible(selection)
&& commands.toggleNode(
&& commands.setNode(
this.name,
'paragraph',
previewAttributesFromSelection(selection),
)
},

/**
* Turn a preview back into a paragraph
* that contains a single link.
*
*/
unsetPreview: () => ({ state, chain }) => {
const { selection } = state
return isPreview(this.name, this.attributes, state)
&& chain()
.setNode('paragraph')
.setLink(this.attributes)
.run()
},

}
},
})
Expand All @@ -101,6 +116,11 @@ function previewAttributesFromSelection({ $from }) {
return { href, title: 'preview' }
}

function isPreview(typeOrName, attributes, state) {
const type = getNodeType(typeOrName, state.schema)
return isNodeActive(state, type, attributes)
}

function previewPossible({ $from }) {
if (childCount($from.parent) > 1) {
return false
Expand Down
4 changes: 3 additions & 1 deletion src/nodes/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import { nodeViewProps, NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2'
import { NcReferenceList } from '@nextcloud/vue/dist/Components/NcRichText.js'
import PreviewOptions from '../components/Editor/PreviewOptions.vue'
import { useEditorMixin } from '../components/Editor.provider.js'
export default {
name: 'Preview',
Expand All @@ -52,13 +53,14 @@ export default {
value: 'link-preview',
}
},
mixins: [ useEditorMixin ],
methods: {
convertToParagraph(...args) {
console.info(...args)
this.$editor.chain()
.focus()
.setTextSelection(this.getPos())
.setNode('paragaph')
.unsetPreview()
.run()
},
},
Expand Down

0 comments on commit f3161a2

Please sign in to comment.