Skip to content

Commit 16c286d

Browse files
authored
fix(custom-uploader): fail 状态插入 \n 会破坏 表格数据结构 使用空格占位 (#345)
1 parent dfadd1d commit 16c286d

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

packages/docs/fluent-editor/demos/file-upload-handle.vue

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import type { Range } from '@opentiny/fluent-editor'
33
import type FluentEditor from '@opentiny/fluent-editor'
44
import { onMounted, ref } from 'vue'
5+
import 'quill-table-up/index.css'
6+
import 'quill-table-up/table-creator.css'
57
68
let editor: FluentEditor
79
const editorRef = ref<HTMLElement>()
@@ -12,30 +14,51 @@ const TOOLBAR_CONFIG = [
1214
[{ list: 'ordered' }, { list: 'bullet' }],
1315
['clean'],
1416
['file', 'image', 'video'],
17+
[{ 'table-up': [] }],
1518
]
1619
1720
onMounted(() => {
1821
// ssr compat, reference: https://vitepress.dev/guide/ssr-compat#importing-in-mounted-hook
19-
import('@opentiny/fluent-editor').then(({ default: FluentEditor }) => {
20-
if (!editorRef.value) return
21-
const Delta = FluentEditor.import('delta')
22-
editor = new FluentEditor(editorRef.value, {
23-
theme: 'snow',
24-
modules: {
25-
toolbar: TOOLBAR_CONFIG,
26-
uploader: {
27-
// only allow image
28-
mimetypes: ['image/*'],
29-
handler(range: Range, files: File[]) {
30-
return files.map((_, i) => i % 2 === 0 ? false : 'https://developer.mozilla.org/static/media/chrome.5e791c51c323fbb93c31.svg')
22+
Promise.all([
23+
import('@opentiny/fluent-editor'),
24+
import('quill-table-up'),
25+
]).then(
26+
([
27+
{ default: FluentEditor, generateTableUp },
28+
{ defaultCustomSelect, TableMenuContextmenu, TableSelection, TableUp },
29+
]) => {
30+
if (!editorRef.value) {
31+
return
32+
}
33+
34+
FluentEditor.register({ 'modules/table-up': generateTableUp(TableUp) }, true)
35+
36+
const Delta = FluentEditor.import('delta')
37+
editor = new FluentEditor(editorRef.value, {
38+
theme: 'snow',
39+
modules: {
40+
'toolbar': TOOLBAR_CONFIG,
41+
'uploader': {
42+
// only allow image
43+
mimetypes: ['image/*'],
44+
handler(range: Range, files: File[]) {
45+
return files.map((_, i) => i % 2 === 0 ? false : 'https://developer.mozilla.org/static/media/chrome.5e791c51c323fbb93c31.svg')
46+
},
47+
fail(file: File, range: Range) {
48+
this.quill.updateContents(new Delta().retain(range.index).delete(1).insert({ image: 'https://developer.mozilla.org/static/media/edge.741dffaf92fcae238b84.svg' }))
49+
},
3150
},
32-
fail(file: File, range: Range) {
33-
this.quill.updateContents(new Delta().retain(range.index).delete(1).insert({ image: 'https://developer.mozilla.org/static/media/edge.741dffaf92fcae238b84.svg' }))
51+
'table-up': {
52+
customSelect: defaultCustomSelect,
53+
selection: TableSelection,
54+
selectionOptions: {
55+
tableMenu: TableMenuContextmenu,
56+
},
3457
},
3558
},
36-
},
37-
})
38-
})
59+
})
60+
},
61+
)
3962
})
4063
</script>
4164

packages/fluent-editor/src/modules/custom-uploader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class FileUploader extends Uploader {
5252
async upload(range: Range, files: FileList | File[]) {
5353
const uploads = []
5454
const fails = []
55+
5556
for (const file of Array.from(files)) {
5657
if (this.validateFile(file)) {
5758
uploads.push(file)
@@ -60,6 +61,7 @@ export class FileUploader extends Uploader {
6061
fails.push(file)
6162
}
6263
}
64+
6365
const result = await this.options.handler.call(this, range, uploads)
6466
const updateDelta = result.reduce((delta, url, i) => {
6567
if (isString(url)) {
@@ -75,15 +77,18 @@ export class FileUploader extends Uploader {
7577
}
7678
}
7779
else {
78-
delta.insert('\n')
80+
delta.insert(' ')
7981
}
8082
return delta
8183
}, new Delta().retain(range.index).delete(range.length))
84+
8285
this.quill.updateContents(updateDelta, Quill.sources.USER)
8386
this.quill.setSelection(range.index + result.length, Quill.sources.SILENT)
87+
8488
for (const file of fails) {
8589
this.options.fail.call(this, file, range)
8690
}
91+
8792
for (const [i, res] of result.entries()) {
8893
if (isString(res)) {
8994
this.options.success.call(this, files[i], { index: range.index + i, length: 0 })

0 commit comments

Comments
 (0)