Skip to content

Commit

Permalink
chore(project): padronize creation in importer and normal context
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Jul 26, 2022
1 parent 5da8d39 commit dc913d6
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 260 deletions.
104 changes: 96 additions & 8 deletions packages/better-write-app/src/store/project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { defineStore } from 'pinia'
import { ContextState, ProjectState, ID, Entity } from 'better-write-types'
import {
ContextState,
ProjectState,
ID,
Entity,
ProjectStateOptions,
} from 'better-write-types'
import { useFormat } from '@/use/format'
import { useGlobalStore } from './global'
import { usePopulate } from '../use/populate'
import { useProject } from '@/use/project'
import { useFactory } from '../use/factory'
import { useUtils } from '@/use/utils'
import { useEnv } from '@/use/env'
import { useDefines } from '@/use/defines'

export const useProjectStore = defineStore('project', {
state: (): ProjectState => {
Expand Down Expand Up @@ -88,17 +97,96 @@ export const useProjectStore = defineStore('project', {
this.bw.version = payload.bw.version
this.shortcuts = payload.shortcuts
},
create(payload: ProjectState, title: string) {
new(options: ProjectStateOptions, forceTitle?: string) {
const global = useGlobalStore()
global.reset()

this.$state = usePopulate().project(payload, title)
},
createExternal(project: ProjectState) {
const global = useGlobalStore()
global.reset()
const title = useUtils()
.text()
.kebab(options.name || '')

this.$state = project
this.$state = {
name: title,
nameRaw: options.name || 'Project',
version: options.version || '0.1.0',
creator: options.creator || 'Better Write',
producer: options.creator || 'betterwrite.io',
keywords: options.keywords || 'docx,project',
subject: options.subject || 'betterwrite',
type: options.type,
base: options.base || 'chapter',
totalPagesCreated: options.totalPagesCreated || 1,
main: options.main || {},
summary: options.summary || {},
pageLoaded: options.pageLoaded || 1,
scrollLoaded: options.scrollLoaded || 0,
offsetLoaded: options.offsetLoaded || 0,
pages: options.pages || [
{
id: 1,
title: forceTitle || title,
entities: [],
createdAt: useFormat().actually(),
updatedAt: useFormat().actually(),
},
],
bw: options.bw || {
platform: 'web',
version: useEnv().packageVersion() as string,
},
pdf: options.pdf || {
encryption: {
userPassword: '',
ownerPassword: '',
},
permissions: {
printing: 'highResolution',
modifying: false,
copying: false,
annotating: true,
fillingForms: true,
contentAccessibility: true,
documentAssembly: true,
},
},
creative: options.creative || {
drafts: [],
},
templates: options.templates || {
generator: [],
substitutions: {
text: useDefines().generator().substitutions().text(),
italic: useDefines().generator().substitutions().italic(),
bold: useDefines().generator().substitutions().bold(),
},
},
shortcuts: options.shortcuts || {
inserts: [
{
key: 'D',
value: '— ',
},
],
},
}

if (this.pages[0].entities.length === 0) {
switch (options.type) {
case 'creative':
this.pages[0].entities.push(
useFactory().entity().create('heading-one', options.name)
)
this.pages[0].entities.push(
useFactory().entity().create('paragraph', '')
)
break
case 'blank':
this.pages[0].entities.push(
useFactory().entity().create('paragraph', '')
)
break
}
}
},
newPage(title: string) {
this.totalPagesCreated++
Expand Down
4 changes: 3 additions & 1 deletion packages/better-write-app/src/use/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const useEditor = () => {
CONTEXT.entities[0].raw === env.emptyLine() ||
entity.utils().isFixed(0)
? title.value
: PROJECT.nameRaw + ' - ' + CONTEXT.entities[0]?.raw
: PROJECT.nameRaw +
(CONTEXT.entities[0]?.raw ? ' - ' : '') +
CONTEXT.entities[0]?.raw
)
useHead({
title: _title,
Expand Down
144 changes: 1 addition & 143 deletions packages/better-write-app/src/use/populate.ts
Original file line number Diff line number Diff line change
@@ -1,153 +1,11 @@
import {
ProjectState,
PDFStateStyles,
DOCXStateFlowItem,
DOCXStateStyles,
} from 'better-write-types'
import { useEnv } from './env'
import { useFormat } from './format'
import { useDefines } from './defines'
import { useUtils } from './utils'
import { useFactory } from './factory'

export const usePopulate = () => {
const factory = useFactory()

const project = (project: ProjectState, title?: string): ProjectState => {
return {
creative: {
name: useUtils().text().kebab(project.name),
nameRaw: project.name,
version: project.version,
creator: project.creator,
producer: project.creator,
keywords: '',
subject: project.subject,
type: project.type,
base: 'chapter',
totalPagesCreated: 1,
main: {},
summary: {},
pageLoaded: 1,
scrollLoaded: 0,
offsetLoaded: 0,
pages: [
{
id: 1,
title: project.name,
entities: [
factory.entity().create('heading-one', title || project.name),
factory.entity().create('paragraph', ''),
],
createdAt: useFormat().actually(),
updatedAt: useFormat().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: useDefines().generator().substitutions().text(),
italic: useDefines().generator().substitutions().italic(),
bold: useDefines().generator().substitutions().bold(),
},
},
shortcuts: {
inserts: [
{
key: 'D',
value: '— ',
},
],
},
},
blank: {
name: useUtils().text().kebab(project.name),
nameRaw: project.name,
version: project.version,
creator: project.creator,
producer: project.creator,
keywords: '',
subject: project.subject,
type: project.type,
totalPagesCreated: 1,
main: {},
summary: {},
pageLoaded: 1,
scrollLoaded: 0,
offsetLoaded: 0,
pages: [
{
id: 1,
title: project.name,
entities: [factory.entity().create('paragraph', '')],
createdAt: useFormat().actually(),
updatedAt: useFormat().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: useDefines().generator().substitutions().text(),
italic: useDefines().generator().substitutions().italic(),
bold: useDefines().generator().substitutions().bold(),
},
},
shortcuts: {
inserts: [
{
key: 'D',
value: '— ',
},
],
},
},
}[project.type] as ProjectState
}

const debug = () => {
const names = () => {
const paragraph = () => {
Expand Down Expand Up @@ -417,5 +275,5 @@ export const usePopulate = () => {
return { styles }
}

return { project, docx, pdf, debug }
return { docx, pdf, debug }
}
9 changes: 5 additions & 4 deletions packages/better-write-app/src/use/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ContextState,
Entity,
Entities,
ProjectStateOptions,
} from 'better-write-types'
import { useStorage } from './storage/storage'
import { useEnv } from './env'
Expand Down Expand Up @@ -56,14 +57,14 @@ export const useProject = () => {
const n = async (type: ProjectType, skipAlert: boolean = false) => {
if (!skipAlert && !confirm(t('toast.project.createAlert'))) return

PROJECT.create(
PROJECT.new(
{
name: t('editor.aside.project.new.content.name'),
version: t('editor.aside.project.new.content.version'),
creator: t('editor.aside.project.new.content.creator'),
subject: t('editor.aside.project.new.content.subject'),
type,
} as any,
},
t('editor.project.control.title', { suffix: 1 })
)

Expand Down Expand Up @@ -91,9 +92,9 @@ export const useProject = () => {
return { new: n }
}

const create = (project: ProjectState) => {
const create = (project: ProjectStateOptions) => {
storage.normalize().then(async () => {
PROJECT.create(project, t('editor.project.control.title', { suffix: 1 }))
PROJECT.new(project, t('editor.project.control.title', { suffix: 1 }))

await nextTick

Expand Down
Loading

0 comments on commit dc913d6

Please sign in to comment.