@lincyaw/workbench 0.2.0-rc.110 → 0.2.0-rc.112
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/workbench",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.112",
|
|
4
4
|
"description": "Presmium's workbench panels in React (the documents, a document edited on its rendered page with comments and version history) with the workspace service's client and the preview protocol's workbench side; @lincyaw/dsh places them in presmium studio (experimental)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"@tiptap/pm": "3.32.0",
|
|
56
56
|
"katex": "0.18.10",
|
|
57
57
|
"react-resizable-panels": "4.14.5",
|
|
58
|
-
"@lincyaw/design": "1.0.0-rc.
|
|
59
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
58
|
+
"@lincyaw/design": "1.0.0-rc.111",
|
|
59
|
+
"@lincyaw/kernel": "1.0.0-rc.111"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"react": "^18.3.1",
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
"@types/react-dom": "18.3.7",
|
|
68
68
|
"react": "^18.3.1",
|
|
69
69
|
"react-dom": "^18.3.1",
|
|
70
|
-
"@lincyaw/compiler": "1.0.0-rc.
|
|
70
|
+
"@lincyaw/compiler": "1.0.0-rc.111"
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const IMPORT_MESSAGES = {
|
|
2
|
+
'zh-CN': {
|
|
3
|
+
title: '导入文档', files: '选择文件', folder: '选择文件夹', submit: '导入',
|
|
4
|
+
description: '支持 Markdown(.md、.markdown)和 Word(.docx)。每个文件生成一份文稿,保留目录层级;一级、二级标题分成页面。',
|
|
5
|
+
images: '含本地图片或文件间链接的 Markdown,请导入所在文件夹。Word 保留标题、段落、列表、表格、强调、链接和内嵌图片。',
|
|
6
|
+
limits: '最多 1000 个文件,合计 32 MiB。导入后可在工作区中继续编辑。',
|
|
7
|
+
selected: (count: number) => `已选择 ${count} 个文件`,
|
|
8
|
+
done: (count: number, unresolved: number, warnings: number) => `已导入 ${count} 份文稿${unresolved ? `,${unresolved} 个链接或图片需要检查` : ''}${warnings ? `,${warnings} 项 Word 格式转换提示需要检查` : ''}。`,
|
|
9
|
+
failed: '导入未完成,请检查文件后重试。',
|
|
10
|
+
errors: {
|
|
11
|
+
'document-import-invalid': '文件路径或选择无效,请重新选择文件或文件夹。',
|
|
12
|
+
'document-import-limit': '文件或转换后的内容超过 1000 个文件、合计 32 MiB 的限制,请分批导入。',
|
|
13
|
+
'document-import-conversion': '文件未能转换,请检查 Markdown 编码或 Word 文件是否完整。',
|
|
14
|
+
'document-import-empty': '没有找到 .md、.markdown 或 .docx 文件。',
|
|
15
|
+
'document-import-unavailable': '请在多文档工作区中导入文件。',
|
|
16
|
+
'document-import-forbidden': '当前工作区不允许导入文件。',
|
|
17
|
+
'document-import-configuration': '工作区配置无效,请先修复配置。',
|
|
18
|
+
'document-import-changed': '工作区在导入期间发生变化,请刷新并检查导入结果。',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
en: {
|
|
22
|
+
title: 'Import Documents', files: 'Choose Files', folder: 'Choose Folder', submit: 'Import',
|
|
23
|
+
description: 'Import Markdown (.md, .markdown) and Word (.docx). Each file becomes a document, keeping its folder hierarchy. Level 1 and 2 headings start pages.',
|
|
24
|
+
images: 'For Markdown with local images or links between files, choose its folder. Word keeps headings, paragraphs, lists, tables, emphasis, links and embedded images.',
|
|
25
|
+
limits: 'Up to 1000 files and 32 MiB in total. Edit the imported documents in this workspace.',
|
|
26
|
+
selected: (count: number) => `${count} files selected`,
|
|
27
|
+
done: (count: number, unresolved: number, warnings: number) => `Imported ${count} documents${unresolved ? `; check ${unresolved} links or images` : ''}${warnings ? `; check ${warnings} Word conversion notices` : ''}.`,
|
|
28
|
+
failed: 'Import did not finish. Check the files and try again.',
|
|
29
|
+
errors: {
|
|
30
|
+
'document-import-invalid': 'Invalid file paths or selection. Choose the files or folder again.',
|
|
31
|
+
'document-import-limit': 'The files or converted content exceed 1000 files or 32 MiB. Import them in smaller batches.',
|
|
32
|
+
'document-import-conversion': 'Conversion failed. Check the Markdown encoding or whether the Word file is complete.',
|
|
33
|
+
'document-import-empty': 'No .md, .markdown or .docx files were found.',
|
|
34
|
+
'document-import-unavailable': 'Open a workspace with multiple documents to import files.',
|
|
35
|
+
'document-import-forbidden': 'This workspace does not allow importing files.',
|
|
36
|
+
'document-import-configuration': 'Fix the workspace configuration before importing.',
|
|
37
|
+
'document-import-changed': 'The workspace changed during import. Refresh and check the imported documents.',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
import { DOCUMENT_IMPORT_MAX_BYTES, DOCUMENT_IMPORT_MAX_FILES } from '@lincyaw/kernel'
|
|
3
|
+
import type { DocumentImportAnswer } from '@lincyaw/kernel'
|
|
4
|
+
import { PmButton, PmDialog, PmNotice } from '../components.tsx'
|
|
5
|
+
import { useMessages, useWorkbench, useWorkbenchState } from '../context.tsx'
|
|
6
|
+
import { IMPORT_MESSAGES } from '../import-messages.ts'
|
|
7
|
+
import { WorkspaceServerError } from '../workspace-client.ts'
|
|
8
|
+
|
|
9
|
+
/** File objects stay local to the dialog until the author submits one import. */
|
|
10
|
+
export function DocumentImportDialog({ onClose, onImported }: { onClose(): void, onImported(answer: DocumentImportAnswer): void }) {
|
|
11
|
+
const t = useMessages()
|
|
12
|
+
const m = IMPORT_MESSAGES[useWorkbenchState(state => state.locale)]
|
|
13
|
+
const workbench = useWorkbench()
|
|
14
|
+
const [files, setFiles] = useState<File[]>([])
|
|
15
|
+
const [working, setWorking] = useState(false)
|
|
16
|
+
const [failure, setFailure] = useState<string>()
|
|
17
|
+
const input = useRef<HTMLInputElement>(null), directory = useRef<HTMLInputElement | null>(null)
|
|
18
|
+
const controller = useRef<AbortController>()
|
|
19
|
+
useEffect(() => () => controller.current?.abort(), [])
|
|
20
|
+
const select = (list: FileList | null) => {
|
|
21
|
+
const selected = [...list ?? []].filter(file => !(file.webkitRelativePath || file.name).split('/').some(part => ['.git', '.presmium', 'node_modules', 'dist'].includes(part)))
|
|
22
|
+
if (selected.length > DOCUMENT_IMPORT_MAX_FILES || selected.reduce((sum, file) => sum + file.size, 0) > DOCUMENT_IMPORT_MAX_BYTES) {
|
|
23
|
+
setFailure(m.errors['document-import-limit']); setFiles([])
|
|
24
|
+
} else { setFiles(selected); setFailure(undefined) }
|
|
25
|
+
}
|
|
26
|
+
const submit = async () => {
|
|
27
|
+
if (working || !files.length) return
|
|
28
|
+
const pending = controller.current = new AbortController()
|
|
29
|
+
setWorking(true); setFailure(undefined)
|
|
30
|
+
try {
|
|
31
|
+
const answer = await workbench.importDocuments(files, pending.signal)
|
|
32
|
+
onImported(answer)
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (!pending.signal.aborted) {
|
|
35
|
+
const code = error instanceof WorkspaceServerError ? error.answer.error : error instanceof Error ? error.message : undefined
|
|
36
|
+
setFailure(code ? (m.errors as Record<string, string>)[code] ?? m.failed : m.failed)
|
|
37
|
+
}
|
|
38
|
+
} finally { if (!pending.signal.aborted) setWorking(false) }
|
|
39
|
+
}
|
|
40
|
+
return (
|
|
41
|
+
<PmDialog open label={m.title} closeLabel={t.close} name="pmw-operation" busy={working} onClose={onClose}>
|
|
42
|
+
<form className="pmw-operation" onSubmit={event => { event.preventDefault(); void submit() }}>
|
|
43
|
+
<p>{m.description}</p><p className="pmw-muted">{m.images}</p><p className="pmw-muted">{m.limits}</p>
|
|
44
|
+
<input ref={input} type="file" multiple hidden accept=".md,.markdown,.docx,image/*" onChange={event => { select(event.currentTarget.files); event.currentTarget.value = '' }} />
|
|
45
|
+
<input ref={element => { directory.current = element; element?.setAttribute('webkitdirectory', '') }} type="file" multiple hidden onChange={event => { select(event.currentTarget.files); event.currentTarget.value = '' }} />
|
|
46
|
+
<div className="pmw-operation__actions">
|
|
47
|
+
<PmButton icon="upload" disabled={working} onClick={() => input.current?.click()}>{m.files}</PmButton>
|
|
48
|
+
<PmButton icon="folder" disabled={working} onClick={() => directory.current?.click()}>{m.folder}</PmButton>
|
|
49
|
+
</div>
|
|
50
|
+
{files.length > 0 && <p>{m.selected(files.length)}</p>}
|
|
51
|
+
{failure && <PmNotice tone="danger">{failure}</PmNotice>}
|
|
52
|
+
<div className="pmw-operation__actions">
|
|
53
|
+
<PmButton disabled={working} onClick={onClose}>{t.cancel}</PmButton>
|
|
54
|
+
<PmButton type="submit" variant="primary" loading={working} disabled={!files.length}>{m.submit}</PmButton>
|
|
55
|
+
</div>
|
|
56
|
+
</form>
|
|
57
|
+
</PmDialog>
|
|
58
|
+
)
|
|
59
|
+
}
|
package/src/panels/HomePanel.tsx
CHANGED
|
@@ -5,6 +5,8 @@ import type { DocumentSummary, ProjectPlan } from '@lincyaw/kernel'
|
|
|
5
5
|
import { PmButton, PmDialog, PmEmpty, PmInput, PmSelect, PmIcon, PmMenu, PmNotice } from '../components.tsx'
|
|
6
6
|
import { useMessages, useWorkbench, useWorkbenchState } from '../context.tsx'
|
|
7
7
|
import { contentText } from '../messages.ts'
|
|
8
|
+
import { IMPORT_MESSAGES } from '../import-messages.ts'
|
|
9
|
+
import { DocumentImportDialog } from './DocumentImportDialog.tsx'
|
|
8
10
|
|
|
9
11
|
type Editing =
|
|
10
12
|
| { op: 'create' }
|
|
@@ -22,6 +24,8 @@ export function HomePanel({ actions }: { actions?: ReactNode } = {}) {
|
|
|
22
24
|
const [done, setDone] = useState<string>()
|
|
23
25
|
const [error, setError] = useState<string>()
|
|
24
26
|
const [working, setWorking] = useState(false)
|
|
27
|
+
const [importing, setImporting] = useState(false)
|
|
28
|
+
const importMessages = IMPORT_MESSAGES[useWorkbenchState(state => state.locale)]
|
|
25
29
|
const handingOff = useRef(false)
|
|
26
30
|
useEffect(() => {
|
|
27
31
|
void workbench.loadDocuments()
|
|
@@ -48,6 +52,7 @@ export function HomePanel({ actions }: { actions?: ReactNode } = {}) {
|
|
|
48
52
|
<header className="pmw-home__bar">
|
|
49
53
|
<h1 id="pmw-home-title" className="pmw-home__title">{answer?.title ?? t.documents}</h1>
|
|
50
54
|
{actions}
|
|
55
|
+
{operable && workbench.client.importDocuments && <PmButton icon="upload" disabled={working || workbench.client.capabilities?.readOnly} onClick={() => setImporting(true)}>{importMessages.title}</PmButton>}
|
|
51
56
|
{operable && <PmButton variant="primary" icon="plus" disabled={working} onClick={() => { handingOff.current = false; setError(undefined); setEditing({ op: 'create' }) }}>{t.newDocument}</PmButton>}
|
|
52
57
|
</header>
|
|
53
58
|
{failure && <PmNotice tone="danger">{bundleDisabled ? t.bundleDisabled : failure}</PmNotice>}
|
|
@@ -91,6 +96,9 @@ export function HomePanel({ actions }: { actions?: ReactNode } = {}) {
|
|
|
91
96
|
))}
|
|
92
97
|
</ul>
|
|
93
98
|
)}
|
|
99
|
+
{importing && <DocumentImportDialog onClose={() => setImporting(false)} onImported={answer => {
|
|
100
|
+
setImporting(false); setQuery(''); setDone(importMessages.done(answer.documents.length, answer.unresolved, answer.warnings))
|
|
101
|
+
}} />}
|
|
94
102
|
{editing && (
|
|
95
103
|
<DocumentDialog
|
|
96
104
|
editing={editing}
|
package/src/workbench.ts
CHANGED
|
@@ -459,6 +459,14 @@ export class Workbench {
|
|
|
459
459
|
})
|
|
460
460
|
}
|
|
461
461
|
|
|
462
|
+
/** Imports a batch, then refreshes the visible document list. */
|
|
463
|
+
async importDocuments(files: readonly File[], signal?: AbortSignal): Promise<import('@lincyaw/kernel').DocumentImportAnswer> {
|
|
464
|
+
if (!this.client.importDocuments) throw new Error('document-import-unavailable')
|
|
465
|
+
const answer = await this.client.importDocuments(files, signal)
|
|
466
|
+
await this.loadDocuments()
|
|
467
|
+
return answer
|
|
468
|
+
}
|
|
469
|
+
|
|
462
470
|
/** Makes a document called `title` from the template, in a directory of its own; answers the directory. */
|
|
463
471
|
async createDocument(title: string, template?: string): Promise<string> {
|
|
464
472
|
const taken = this.#state.documents?.documents.map(doc => doc.dir) ?? []
|
package/src/workspace-client.ts
CHANGED
|
@@ -79,6 +79,7 @@ export interface WorkspaceClient {
|
|
|
79
79
|
openAuthoringWindow?: AuthoringWindowSubscription
|
|
80
80
|
subscribeRequests?(listener: (event: WorkspaceRequestEvent) => void): () => void
|
|
81
81
|
readDocuments(document?: string): Promise<DocumentsAnswer>
|
|
82
|
+
importDocuments?(files: readonly File[], signal?: AbortSignal): Promise<import('@lincyaw/kernel').DocumentImportAnswer>
|
|
82
83
|
planProjectChange(request: ProjectPlanRequest): Promise<ProjectPlan>
|
|
83
84
|
operateDocument(operation: ProjectOperation, planned?: ProjectPlan): Promise<OperationAnswer>
|
|
84
85
|
readSource(document: string, path: string): Promise<SourceAnswer>
|
|
@@ -202,7 +203,7 @@ export function workspaceClient(base = 'api/presmium/', subscription?: Workspace
|
|
|
202
203
|
const path = target.pathname.slice(prefix.length)
|
|
203
204
|
const method = (init?.method ?? 'GET').toUpperCase()
|
|
204
205
|
if (client.capabilities?.readOnly && !['GET', 'HEAD', 'OPTIONS'].includes(method)
|
|
205
|
-
&& /^(?:
|
|
206
|
+
&& /^(?:documents?(?:\/|$)|source(?:\/|$)|project\/|pages\/|history\/|revisions\/|review(?:\/|$))/.test(path)) throw new WorkspaceServerError(403, { error: 'permission-denied' })
|
|
206
207
|
if (path === 'telemetry' || (path === 'diagnostics' || path === 'diagnostics/authoring') && method === 'POST') return fetch(address, { credentials: 'same-origin', ...init })
|
|
207
208
|
const document = target.searchParams.get('document') ?? undefined
|
|
208
209
|
const requestId = ++requestSequence
|
|
@@ -370,6 +371,12 @@ export function workspaceClient(base = 'api/presmium/', subscription?: Workspace
|
|
|
370
371
|
throw error
|
|
371
372
|
}
|
|
372
373
|
},
|
|
374
|
+
importDocuments: (files, signal) => {
|
|
375
|
+
const body = new FormData()
|
|
376
|
+
body.set('paths', JSON.stringify(files.map(file => file.webkitRelativePath || file.name)))
|
|
377
|
+
for (const file of files) body.append('files', file, file.name)
|
|
378
|
+
return call(url('documents/import'), { method: 'POST', body, signal })
|
|
379
|
+
},
|
|
373
380
|
readSource: (document, path) => call(url('source', document, { path })),
|
|
374
381
|
saveSource: (document, path, content, base, message) => post(url('source', document), { path, content, base, ...message ? { message } : {} }),
|
|
375
382
|
saveSourceTransaction: (document, changes, message) => post(url('source/transaction', document), { changes, ...message ? { message } : {} }),
|