-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! fix(files): add "Link to file or folder" and "Upload" buttons
Signed-off-by: julia.kirschenheuter <[email protected]>
- Loading branch information
1 parent
dcdd7c7
commit bc78ba4
Showing
4 changed files
with
88 additions
and
114 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 |
---|---|---|
|
@@ -65,8 +65,6 @@ | |
|
||
<script> | ||
import { NcActions, NcActionButton, NcActionInput } from '@nextcloud/vue' | ||
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js' | ||
import { FilePickerType, getFilePickerBuilder } from '@nextcloud/dialogs' | ||
import { generateUrl } from '@nextcloud/router' | ||
import { loadState } from '@nextcloud/initial-state' | ||
|
||
|
@@ -76,6 +74,7 @@ import { Document, Loading, LinkOff, Web, Shape } from '../icons.js' | |
import { BaseActionEntry } from './BaseActionEntry.js' | ||
import { useFileMixin } from '../Editor.provider.js' | ||
import { useMenuIDMixin } from './MenuBar.provider.js' | ||
import { buildFilePicker } from '../../helpers/filePicker.js' | ||
|
||
export default { | ||
name: 'ActionInsertLink', | ||
|
@@ -122,12 +121,7 @@ export default { | |
this.startPath = this.relativePath.split('/').slice(0, -1).join('/') | ||
} | ||
|
||
const filePicker = getFilePickerBuilder(t('text', 'Select file or folder to link to')) | ||
.startAt(this.startPath) | ||
.allowDirectories(true) | ||
.setMultiSelect(false) | ||
.setType(FilePickerType.Choose) | ||
.build() | ||
const filePicker = buildFilePicker(this.startPath) | ||
|
||
filePicker.pick() | ||
.then((file) => { | ||
|
@@ -173,42 +167,9 @@ export default { | |
* @param {string} text Text part of the link | ||
*/ | ||
setLink(url, text) { | ||
// Heuristics for determining if we need a https:// prefix. | ||
const noPrefixes = [ | ||
/^[a-zA-Z]+:/, // url with protocol ("mailTo:[email protected]") | ||
/^\//, // absolute path | ||
/\?fileId=/, // relative link with fileId | ||
/^\.\.?\//, // relative link starting with ./ or ../ | ||
/^[^.]*[/$]/, // no dots before first '/' - not a domain name | ||
/^#/, // url fragment | ||
] | ||
if (url && !noPrefixes.find(regex => url.match(regex))) { | ||
url = 'https://' + url | ||
} | ||
|
||
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: [email protected]") | ||
const href = url.replaceAll(' ', '%20') | ||
const chain = this.$editor.chain() | ||
// Check if any text is selected, if not insert the link using the given text property | ||
if (this.$editor.view.state?.selection.empty) { | ||
chain.insertContent({ | ||
type: 'paragraph', | ||
content: [{ | ||
type: 'text', | ||
marks: [{ | ||
type: 'link', | ||
attrs: { | ||
href, | ||
}, | ||
}], | ||
text, | ||
}], | ||
}) | ||
} else { | ||
chain.setLink({ href }) | ||
} | ||
chain.focus().run() | ||
this.$editor.chain().setOrInsertLink(url, text).focus().run() | ||
}, | ||
|
||
/** | ||
* Remove link markup at current position | ||
* Triggered by the "remove link" button | ||
|
@@ -218,18 +179,7 @@ export default { | |
this.menuOpen = false | ||
}, | ||
linkPicker() { | ||
getLinkWithPicker(null, true) | ||
.then(link => { | ||
const chain = this.$editor.chain() | ||
if (this.$editor.view.state?.selection.empty) { | ||
chain.focus().insertPreview(link).run() | ||
} else { | ||
chain.setLink({ href: link }).focus().run() | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Smart picker promise rejected', error) | ||
}) | ||
this.$editor.chain().getLinkPicker().focus().run() | ||
}, | ||
}, | ||
} | ||
|
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 |
---|---|---|
|
@@ -43,10 +43,10 @@ | |
import { NcButton } from '@nextcloud/vue' | ||
import { Document, Table, Shape } from './icons.js' | ||
import { useActionChooseLocalAttachmentMixin } from './Editor/MediaHandler.provider.js' | ||
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js' | ||
import { useEditorMixin, useFileMixin } from './Editor.provider.js' | ||
import { FilePickerType, getFilePickerBuilder } from '@nextcloud/dialogs' | ||
import { generateUrl } from '@nextcloud/router' | ||
import { buildFilePicker } from '../helpers/filePicker.js' | ||
export default { | ||
name: 'SuggestionsBar', | ||
|
@@ -75,23 +75,8 @@ export default { | |
}, | ||
methods: { | ||
/** | ||
* Open smart picker dialog | ||
* Triggered by the "Smart Picker" button | ||
*/ | ||
linkPicker() { | ||
getLinkWithPicker(null, true) | ||
.then(link => { | ||
const chain = this.$editor.chain() | ||
if (this.$editor.view.state?.selection.empty) { | ||
chain.focus().insertPreview(link).run() | ||
} else { | ||
chain.setLink({ href: link }).focus().run() | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Smart picker promise rejected', error) | ||
}) | ||
this.$editor.chain().getLinkPicker().focus().run() | ||
}, | ||
/** | ||
|
@@ -111,12 +96,7 @@ export default { | |
this.startPath = this.relativePath.split('/').slice(0, -1).join('/') | ||
} | ||
const filePicker = getFilePickerBuilder(t('text', 'Select file or folder to link to')) | ||
.startAt(this.startPath) | ||
.allowDirectories(true) | ||
.setMultiSelect(false) | ||
.setType(FilePickerType.Choose) | ||
.build() | ||
const filePicker = buildFilePicker(this.startPath) | ||
filePicker.pick() | ||
.then((file) => { | ||
|
@@ -141,41 +121,7 @@ export default { | |
* @param {string} text Text part of the link | ||
*/ | ||
setLink(url, text) { | ||
// Heuristics for determining if we need a https:// prefix. | ||
const noPrefixes = [ | ||
/^[a-zA-Z]+:/, // url with protocol ("mailTo:[email protected]") | ||
/^\//, // absolute path | ||
/\?fileId=/, // relative link with fileId | ||
/^\.\.?\//, // relative link starting with ./ or ../ | ||
/^[^.]*[/$]/, // no dots before first '/' - not a domain name | ||
/^#/, // url fragment | ||
] | ||
if (url && !noPrefixes.find(regex => url.match(regex))) { | ||
url = 'https://' + url | ||
} | ||
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: [email protected]") | ||
const href = url.replaceAll(' ', '%20') | ||
const chain = this.$editor.chain() | ||
// Check if any text is selected, if not insert the link using the given text property | ||
if (this.$editor.view.state?.selection.empty) { | ||
chain.insertContent({ | ||
type: 'paragraph', | ||
content: [{ | ||
type: 'text', | ||
marks: [{ | ||
type: 'link', | ||
attrs: { | ||
href, | ||
}, | ||
}], | ||
text, | ||
}], | ||
}) | ||
} else { | ||
chain.setLink({ href }) | ||
} | ||
chain.focus().run() | ||
this.$editor.chain().setOrInsertLink(url, text).focus().run() | ||
}, | ||
}, | ||
|
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,15 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { FilePickerType, getFilePickerBuilder } from '@nextcloud/dialogs' | ||
|
||
export const buildFilePicker = (startPath) => { | ||
return getFilePickerBuilder(t('text', 'Select file or folder to link to')) | ||
.startAt(startPath) | ||
.allowDirectories(true) | ||
.setMultiSelect(false) | ||
.setType(FilePickerType.Choose) | ||
.build() | ||
} | ||
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ import { markInputRule } from '@tiptap/core' | |
import TipTapLink from '@tiptap/extension-link' | ||
import { domHref, parseHref } from './../helpers/links.js' | ||
import { linkClicking } from '../plugins/links.js' | ||
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js' | ||
|
||
const PROTOCOLS_TO_LINK_TO = ['http:', 'https:', 'mailto:', 'tel:'] | ||
|
||
|
@@ -88,6 +89,68 @@ const Link = TipTapLink.extend({ | |
] | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
/** | ||
* Check if any text is selected, if not insert the link using the given text property | ||
* | ||
* @param {string} url href attribute of the link | ||
* @param {string} text Text part of the link | ||
*/ | ||
setOrInsertLink: (url, text) => ({ state, chain }) => { | ||
// Heuristics for determining if we need a https:// prefix. | ||
const noPrefixes = [ | ||
/^[a-zA-Z]+:/, // url with protocol ("mailTo:[email protected]") | ||
/^\//, // absolute path | ||
/\?fileId=/, // relative link with fileId | ||
/^\.\.?\//, // relative link starting with ./ or ../ | ||
/^[^.]*[/$]/, // no dots before first '/' - not a domain name | ||
/^#/, // url fragment | ||
] | ||
if (url && !noPrefixes.find(regex => url.match(regex))) { | ||
url = 'https://' + url | ||
} | ||
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: [email protected]") | ||
const href = url.replaceAll(' ', '%20') | ||
if (state.selection.empty) { | ||
return chain().insertContent({ | ||
type: 'paragraph', | ||
content: [{ | ||
type: 'text', | ||
marks: [{ | ||
type: 'link', | ||
attrs: { | ||
href, | ||
}, | ||
}], | ||
text, | ||
}], | ||
}).run() | ||
} else { | ||
return chain().setLink({ href }).run() | ||
} | ||
}, | ||
|
||
/** | ||
* Open smart picker dialog | ||
* Triggered by the "Smart Picker" button | ||
*/ | ||
getLinkPicker: () => ({ state, chain }) => { | ||
getLinkWithPicker(null, true) | ||
.then(link => { | ||
if (state.selection.empty) { | ||
return chain().focus().insertPreview(link).run() | ||
} else { | ||
return chain().setLink({ href: link }).focus().run() | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Smart picker promise rejected', error) | ||
}) | ||
}, | ||
} | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
const plugins = this.parent() | ||
// remove upstream link click handle plugin | ||
|