Skip to content

Commit a9a7d13

Browse files
committed
feat(importer): txt support
1 parent babd1d7 commit a9a7d13

File tree

3 files changed

+112
-9
lines changed

3 files changed

+112
-9
lines changed

packages/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
```
44
├── better-write-app # Website
5+
|── better-write-importer # Converter for some extensions -> .bw
56
├── better-write-localisation # i18n
67
├── better-write-plugin-core # Plugin base core
78
├── better-write-plugin-docx # DOCX Generator Plugin
89
├── better-write-plugin-logger # Logger Plugin
10+
├── better-write-plugin-multiplayer # liveblocks.io Plugin
911
├── better-write-plugin-pdf # PDF Generator Plugin
1012
├── better-write-plugin-theme # Multi-Theme Plugin
1113
├── better-write-plugin-txt # TXT Generator Plugin

packages/better-write-app/src/use/project.ts

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export const useProject = () => {
136136

137137
if (!breakpoints.isMobile().value) ABSOLUTE.aside = true
138138

139+
ABSOLUTE.project.blocked = false
140+
139141
const editor = document.querySelector('#edit')
140142

141143
if (editor) editor.scrollTop = PROJECT.scrollLoaded
@@ -191,7 +193,7 @@ export const useProject = () => {
191193
.finally(() => {})
192194
}
193195

194-
const onLoadImporter = async (content: ImporterData, fileName: string) => {
196+
const onLoadDOCX = async (content: ImporterData, fileName: string) => {
195197
const entities: Entities = []
196198

197199
content.entities.forEach(({ type, raw }) => {
@@ -290,20 +292,115 @@ export const useProject = () => {
290292
if (!breakpoints.isMobile().value && PROJECT.type === 'creative')
291293
ABSOLUTE.aside = true
292294

295+
ABSOLUTE.project.blocked = false
296+
297+
toast.success(t('toast.project.create'))
298+
}
299+
300+
const onLoadTXT = async (data: string, filename: string) => {
301+
const content = data.split('\n')
302+
const entities: Entities = []
303+
let __FIRST_ROW__ = true
304+
305+
content.forEach((row) => {
306+
if (__FIRST_ROW__ && row) {
307+
entities.push(factory.entity().create('heading-one', row))
308+
__FIRST_ROW__ = false
309+
310+
return
311+
}
312+
313+
if (!row) {
314+
entities.push(factory.entity().create('line-break'))
315+
316+
return
317+
}
318+
319+
entities.push(factory.entity().create('paragraph', row))
320+
})
321+
322+
PROJECT.createExternal({
323+
name: ut.text().kebab(filename),
324+
nameRaw: filename,
325+
version: '0.1.0',
326+
creator: 'betterwrite',
327+
producer: 'betterwrite',
328+
keywords: 'docx,project',
329+
subject: 'betterwrite',
330+
type: 'creative',
331+
totalPagesCreated: 1,
332+
main: {},
333+
summary: {},
334+
pageLoaded: 1,
335+
scrollLoaded: 0,
336+
offsetLoaded: 0,
337+
pages: [
338+
{
339+
id: 1,
340+
title: entities[0].raw,
341+
entities,
342+
createdAt: format.actually(),
343+
updatedAt: format.actually(),
344+
},
345+
],
346+
bw: {
347+
platform: 'web',
348+
version: useEnv().packageVersion() as string,
349+
},
350+
pdf: {
351+
encryption: {
352+
userPassword: '',
353+
ownerPassword: '',
354+
},
355+
permissions: {
356+
printing: 'highResolution',
357+
modifying: false,
358+
copying: false,
359+
annotating: true,
360+
fillingForms: true,
361+
contentAccessibility: true,
362+
documentAssembly: true,
363+
},
364+
},
365+
creative: {
366+
drafts: [],
367+
},
368+
templates: {
369+
generator: [],
370+
substitutions: {
371+
text: defines.generator().substitutions().text(),
372+
italic: defines.generator().substitutions().italic(),
373+
bold: defines.generator().substitutions().bold(),
374+
},
375+
},
376+
})
377+
378+
await nextTick
379+
380+
CONTEXT.load(PROJECT.pages[0])
381+
382+
await nextTick
383+
384+
if (!breakpoints.isMobile().value && PROJECT.type === 'creative')
385+
ABSOLUTE.aside = true
386+
387+
ABSOLUTE.project.blocked = false
388+
293389
toast.success(t('toast.project.create'))
294390
}
295391

296392
const onImportProject = () => {
297393
const _ = document.createElement('input')
298394
_.type = 'file'
299-
_.accept = '.doc,.docx,.bw'
395+
_.accept = '.doc,.docx,.bw,.txt'
300396
_.addEventListener('change', function () {
301397
const file: File = (this.files as any)[0]
302398

303399
if (!file) return
304400

305401
const isDoc = file.name.endsWith('.doc') || file.name.endsWith('.docx')
306402
const isBW = file.name.endsWith('.bw')
403+
const isTXT = file.name.endsWith('.txt')
307404

308405
const reader = new FileReader()
309406
isDoc ? reader.readAsDataURL(file) : reader.readAsText(file)
@@ -316,19 +413,23 @@ export const useProject = () => {
316413
return
317414
}
318415

416+
const filename = file.name
417+
.replace('.bw', '')
418+
.replace('.docx', '')
419+
.replace('.doc', '')
420+
.replace('.txt', '')
421+
319422
if (isDoc) {
320423
const importers = await DocxToJson(reader.result as string)
321424

322-
onLoadImporter(
323-
importers,
324-
file.name
325-
.replace('.bw', '')
326-
.replace('.docx', '')
327-
.replace('.doc', '')
328-
)
425+
onLoadDOCX(importers, filename)
329426

330427
return
331428
}
429+
430+
if (isTXT) {
431+
await onLoadTXT(reader.result as string, filename)
432+
}
332433
}
333434
reader.onerror = function (err) {}
334435
})

packages/better-write-importer/src/converter.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)