Skip to content

Commit

Permalink
feat(importer): txt support
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed May 19, 2022
1 parent babd1d7 commit a9a7d13
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

```
├── better-write-app # Website
|── better-write-importer # Converter for some extensions -> .bw
├── better-write-localisation # i18n
├── better-write-plugin-core # Plugin base core
├── better-write-plugin-docx # DOCX Generator Plugin
├── better-write-plugin-logger # Logger Plugin
├── better-write-plugin-multiplayer # liveblocks.io Plugin
├── better-write-plugin-pdf # PDF Generator Plugin
├── better-write-plugin-theme # Multi-Theme Plugin
├── better-write-plugin-txt # TXT Generator Plugin
Expand Down
119 changes: 110 additions & 9 deletions packages/better-write-app/src/use/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export const useProject = () => {

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

ABSOLUTE.project.blocked = false

const editor = document.querySelector('#edit')

if (editor) editor.scrollTop = PROJECT.scrollLoaded
Expand Down Expand Up @@ -191,7 +193,7 @@ export const useProject = () => {
.finally(() => {})
}

const onLoadImporter = async (content: ImporterData, fileName: string) => {
const onLoadDOCX = async (content: ImporterData, fileName: string) => {
const entities: Entities = []

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

ABSOLUTE.project.blocked = false

toast.success(t('toast.project.create'))
}

const onLoadTXT = async (data: string, filename: string) => {
const content = data.split('\n')
const entities: Entities = []
let __FIRST_ROW__ = true

content.forEach((row) => {
if (__FIRST_ROW__ && row) {
entities.push(factory.entity().create('heading-one', row))
__FIRST_ROW__ = false

return
}

if (!row) {
entities.push(factory.entity().create('line-break'))

return
}

entities.push(factory.entity().create('paragraph', row))
})

PROJECT.createExternal({
name: ut.text().kebab(filename),
nameRaw: filename,
version: '0.1.0',
creator: 'betterwrite',
producer: 'betterwrite',
keywords: 'docx,project',
subject: 'betterwrite',
type: 'creative',
totalPagesCreated: 1,
main: {},
summary: {},
pageLoaded: 1,
scrollLoaded: 0,
offsetLoaded: 0,
pages: [
{
id: 1,
title: entities[0].raw,
entities,
createdAt: format.actually(),
updatedAt: format.actually(),
},
],
bw: {
platform: 'web',
version: useEnv().packageVersion() as string,
},
pdf: {
encryption: {
userPassword: '',
ownerPassword: '',
},
permissions: {
printing: 'highResolution',
modifying: false,
copying: false,
annotating: true,
fillingForms: true,
contentAccessibility: true,
documentAssembly: true,
},
},
creative: {
drafts: [],
},
templates: {
generator: [],
substitutions: {
text: defines.generator().substitutions().text(),
italic: defines.generator().substitutions().italic(),
bold: defines.generator().substitutions().bold(),
},
},
})

await nextTick

CONTEXT.load(PROJECT.pages[0])

await nextTick

if (!breakpoints.isMobile().value && PROJECT.type === 'creative')
ABSOLUTE.aside = true

ABSOLUTE.project.blocked = false

toast.success(t('toast.project.create'))
}

const onImportProject = () => {
const _ = document.createElement('input')
_.type = 'file'
_.accept = '.doc,.docx,.bw'
_.accept = '.doc,.docx,.bw,.txt'
_.addEventListener('change', function () {
const file: File = (this.files as any)[0]

if (!file) return

const isDoc = file.name.endsWith('.doc') || file.name.endsWith('.docx')
const isBW = file.name.endsWith('.bw')
const isTXT = file.name.endsWith('.txt')

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

const filename = file.name
.replace('.bw', '')
.replace('.docx', '')
.replace('.doc', '')
.replace('.txt', '')

if (isDoc) {
const importers = await DocxToJson(reader.result as string)

onLoadImporter(
importers,
file.name
.replace('.bw', '')
.replace('.docx', '')
.replace('.doc', '')
)
onLoadDOCX(importers, filename)

return
}

if (isTXT) {
await onLoadTXT(reader.result as string, filename)
}
}
reader.onerror = function (err) {}
})
Expand Down
Empty file.

0 comments on commit a9a7d13

Please sign in to comment.