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

[stable25] Fix: relative link in RichWorkspace #3323

Merged
merged 3 commits into from
Nov 7, 2022
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
46 changes: 43 additions & 3 deletions cypress/e2e/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { randHash } from '../utils/index.js'
const randUser = randHash()

describe('Workspace', function() {
let currentFolder

before(function() {
cy.nextcloudCreateUser(randUser, 'password')
Expand All @@ -33,11 +34,12 @@ describe('Workspace', function() {
cy.login(randUser, 'password').then(() => {
// isolate tests - each happens in its own folder
const retry = cy.state('test').currentRetry()
const folderName = retry

currentFolder = retry
? `${Cypress.currentTest.title} (${retry})`
: Cypress.currentTest.title
cy.createFolder(folderName)
cy.visit(`apps/files?dir=/${encodeURIComponent(folderName)}`)
cy.createFolder(currentFolder)
cy.visit(`apps/files?dir=/${encodeURIComponent(currentFolder)}`)
})
})

Expand Down Expand Up @@ -169,6 +171,44 @@ describe('Workspace', function() {
.contains('😀')
})

it('relative folder links', () => {
cy.createFolder(`${currentFolder}/sub-folder`)
cy.createFolder(`${currentFolder}/sub-folder/alpha`)

cy.uploadFile('test.md', 'text/markdown', `${currentFolder}/sub-folder/alpha/test.md`)

cy.openWorkspace()
.type('link me')
.type('{selectall}')

menuBubbleButton('add-file')
.click()

cy.get('#picker-filestable tr[data-entryname="sub-folder"]').click()
cy.get('#picker-filestable tr[data-entryname="alpha"]').click()
cy.get('#picker-filestable tr[data-entryname="test.md"]').click()
cy.get('.oc-dialog > .oc-dialog-buttonrow button').click()

cy.getEditor()
.find('a')
.should('have.attr', 'href')
.and('contains', `dir=/${currentFolder}/sub-folder/alpha`)
.and('contains', '#relPath=sub-folder/alpha/test.md')

cy.getEditor()
.find('a').click()

cy.getModal()
.find('.modal-header')
.contains('test.md')

cy.getModal()
.getEditor()
.contains('Hello world')

cy.getModal().find('button.header-close').click()
})

describe('callouts', () => {
const types = ['info', 'warn', 'error', 'success']

Expand Down
4 changes: 2 additions & 2 deletions js/editor-rich.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor-rich.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files-modal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/files-modal.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ const loadSyntaxHighlight = async (language) => {
}
}

const createEditor = ({ content, onCreate, onUpdate, extensions, enableRichEditing, session }) => {
const createEditor = ({ content, onCreate, onUpdate, extensions, enableRichEditing, session, relativePath }) => {
let richEditingExtensions = []
if (enableRichEditing) {
richEditingExtensions = [
Markdown,
RichText.configure({
relativePath,
extensions: [
EditableTable,
Mention.configure({
Expand Down
1 change: 1 addition & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ export default {
(this.isRichEditor ? Promise.resolve() : loadSyntaxHighlight(language))
.then(() => {
this.$editor = createEditor({
relativePath: this.relativePath,
session: this.currentSession,
content,
onCreate: ({ editor }) => {
Expand Down
1 change: 1 addition & 0 deletions src/extensions/RichText.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default Extension.create({
...this.options.link,
openOnClick: true,
validate: href => /^https?:\/\//.test(href),
relativePath: this.options.relativePath,
}))
}
const additionalExtensionNames = this.options.extensions.map(e => e.name)
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const basedir = function(file) {
: file.slice(0, end + 1) // basedir('/toplevel') should return '/'
}

const domHref = function(node) {
const domHref = function(node, relativePath) {
const ref = node.attrs.href
if (!ref) {
return ref
Expand All @@ -61,10 +61,11 @@ const domHref = function(node) {
if (ref.startsWith('#')) {
return ref
}

const match = ref.match(/^([^?]*)\?fileId=(\d+)/)
if (match) {
const [, relPath, id] = match
const currentDir = basedir(OCA.Viewer.file)
const currentDir = basedir(relativePath || OCA.Viewer.file)
const dir = absolutePath(currentDir, basedir(relPath))
return generateUrl(`/apps/files/?dir=${dir}&openfile=${id}#relPath=${relPath}`)
}
Expand Down
15 changes: 10 additions & 5 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Link = TipTapLink.extend({
return {
...this.parent?.(),
onClick: openLink,
relativePath: null,
}
},

Expand All @@ -56,11 +57,15 @@ const Link = TipTapLink.extend({
},
],

renderHTML: ({ mark, HTMLAttributes }) => ['a', {
...mark.attrs,
href: domHref(mark),
rel: 'noopener noreferrer nofollow',
}, 0],
renderHTML(options) {
const { mark } = options

return ['a', {
...mark.attrs,
href: domHref(mark, this.options.relativePath),
rel: 'noopener noreferrer nofollow',
}, 0]
},

addProseMirrorPlugins() {
const plugins = this.parent()
Expand Down