-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: grnd-alt <[email protected]>
- Loading branch information
Showing
3 changed files
with
131 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,6 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/** | ||
* @copyright Copyright (c) 2024 Max <[email protected]> | ||
* | ||
* @author Max <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import Markdown from './../../../src/extensions/Markdown.js' | ||
|
@@ -27,68 +9,37 @@ import { createCustomEditor } from './../../support/components.js' | |
import { loadMarkdown, expectMarkdown } from '../nodes/helpers.js' | ||
|
||
describe('Link marks', { retries: 0 }, () => { | ||
|
||
const editor = createCustomEditor({ | ||
content: '', | ||
extensions: [ | ||
Markdown, | ||
Link, | ||
Italic, | ||
], | ||
extensions: [Markdown, Link, Italic], | ||
}) | ||
|
||
describe('insertOrSetLink command', { retries: 0 }, () => { | ||
|
||
it('is available in commands', () => { | ||
expect(editor.commands).to.have.property('insertOrSetLink') | ||
}) | ||
|
||
it('can run on normal paragraph', () => { | ||
prepareEditor('hello\n', 3) | ||
expect(editor.can().insertOrSetLink()).to.be.ok | ||
expect(editor.can().insertOrSetLink()).toBe(true) | ||
}) | ||
|
||
it('will insert a link in a normal paragraph', () => { | ||
prepareEditor('hello\n', 3) | ||
editor.commands.insertOrSetLink('https://nextcloud.com', { href: 'https://nextcloud.com' }) | ||
editor.commands.insertOrSetLink('https://nextcloud.com', { | ||
href: 'https://nextcloud.com', | ||
}) | ||
expectMarkdown(editor, 'he\n\n<https://nextcloud.com>\n\nllo') | ||
}) | ||
|
||
}) | ||
|
||
/** | ||
* Expect a link in the editor. | ||
*/ | ||
function expectLink() { | ||
expect(getParentNode().type.name).to.equal('paragraph') | ||
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com') | ||
expect(getMark().attrs.href).to.equal('https://nextcloud.com') | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
function getParentNode() { | ||
const { state: { selection } } = editor | ||
return selection.$head.parent | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
function getMark() { | ||
const { state: { selection } } = editor | ||
console.info(selection.$head) | ||
return selection.$head.nodeAfter.marks[0] | ||
} | ||
|
||
/** | ||
* | ||
* @param input | ||
* @param {*} input markdown content | ||
* @param {*} position cursor pos | ||
*/ | ||
function prepareEditor(input, position = 1) { | ||
loadMarkdown(editor, input) | ||
editor.commands.setTextSelection( position ) | ||
editor.commands.setTextSelection(position) | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import Link from './../../marks/Link.js' | ||
import Underline from '../../marks/Underline.js' | ||
import createCustomEditor from '../testHelpers/createCustomEditor.ts' | ||
|
||
describe('Link extension integrated in the editor', () => { | ||
it('should have link available in commands', () => { | ||
const editor = createCustomEditor('<p><a href="nextcloud.com">Test</a> HELLO WORLD</p>', [Link]) | ||
expect(editor.commands).toHaveProperty('insertOrSetLink') | ||
}) | ||
|
||
it('should update link if anchor has mark', () => { | ||
const editor = createCustomEditor( | ||
'<p><a href="nextcloud.com">Te<u>s</u>t</a> HELLO WORLD</p>', | ||
[Link, Underline], | ||
) | ||
editor.commands.setTextSelection(3) | ||
editor.commands.insertOrSetLink('updated.de', { href: 'updated.de' }) | ||
expect(editor.getJSON()).toEqual({ | ||
content: [ | ||
{ | ||
content: [ | ||
{ | ||
marks: [ | ||
{ attrs: { href: 'updated.de', title: null }, type: 'link' }, | ||
], | ||
text: 'updated.de', | ||
type: 'text', | ||
}, | ||
{ text: ' HELLO WORLD', type: 'text' }, | ||
], | ||
type: 'paragraph', | ||
}, | ||
], | ||
type: 'doc', | ||
}) | ||
}) | ||
|
||
it('Should only update link the anchor is on', () => { | ||
const editor = createCustomEditor( | ||
'<p><a href="nextcloud.com">Test</a><a href="not-nextcloud.com">second link</a></p>', | ||
[Link], | ||
) | ||
editor.commands.setTextSelection(3) | ||
editor.commands.insertOrSetLink('updated.de', { href: 'updated.de' }) | ||
expect(editor.getJSON()).toEqual({ | ||
content: [ | ||
{ | ||
content: [ | ||
{ | ||
marks: [ | ||
{ attrs: { href: 'updated.de', title: null }, type: 'link' }, | ||
], | ||
text: 'updated.de', | ||
type: 'text', | ||
}, | ||
{ | ||
"marks": [ | ||
Check failure on line 61 in src/tests/marks/Link.spec.js
|
||
{ | ||
attrs: { | ||
href: "not-nextcloud.com", | ||
"title": null, | ||
Check failure on line 65 in src/tests/marks/Link.spec.js
|
||
}, | ||
type: "link", | ||
} | ||
], | ||
text: "second link", | ||
type: "text", | ||
} | ||
], | ||
type: 'paragraph', | ||
}, | ||
], | ||
type: 'doc', | ||
}) | ||
}) | ||
|
||
it('should insert new link if none at anchor', () => { | ||
const editor = createCustomEditor( | ||
'<p><a href="nextcloud.com">Test</a> HELLO WORLD</p>', | ||
[Link], | ||
) | ||
editor.commands.setTextSelection(10) | ||
expect(editor.getJSON()).toEqual({ | ||
content: [ | ||
{ | ||
content: [ | ||
{ | ||
marks: [ | ||
{ attrs: { href: 'nextcloud.com', title: null }, type: 'link' }, | ||
], | ||
text: 'Test', | ||
type: 'text', | ||
}, | ||
{ text: ' HELLO WORLD', type: 'text' }, | ||
], | ||
type: 'paragraph', | ||
}, | ||
], | ||
type: 'doc', | ||
}) | ||
}) | ||
}) |