Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 119bca4

Browse files
authored
Make sure deprecated document types can be read by Mazed today (#388)
1 parent 5a95053 commit 119bca4

File tree

4 files changed

+93
-28
lines changed

4 files changed

+93
-28
lines changed

elementary/src/editor/types.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { makeParagraph, makeLeaf, TDoc } from './types'
1+
import { makeSlateTextFromPlainText, TDoc } from './types'
22

33
test('exctractDocTitle - raw string', async () => {
44
const text = 'RmdBzaGUgdHJpZWQgdG8gd2FzaCBvZm'
5-
const doc = new TDoc([makeParagraph([makeLeaf(text)])])
5+
const doc = new TDoc(makeSlateTextFromPlainText(text))
66
const title = doc.genTitle()
77
expect(title).toStrictEqual(text)
88
})
99

1010
test('exctractDocTitle - empty object', () => {
11-
const doc = new TDoc([makeParagraph([makeLeaf('')])])
11+
const doc = new TDoc(makeSlateTextFromPlainText())
1212
const title = doc.genTitle()
1313
expect(title).toStrictEqual('…')
1414
})
1515

1616
test('exctractDocTitle - make sure wide (unicode) characters are not cut in half', () => {
1717
const ustr = '☀️🐥📦⏳☕️💡🏗'.repeat(100)
18-
const doc = new TDoc([makeParagraph([makeLeaf(ustr)])])
18+
const doc = new TDoc(makeSlateTextFromPlainText(ustr))
1919
const title = doc.genTitle(19)
2020
expect(title).toStrictEqual('☀️🐥📦⏳☕️💡🏗☀️🐥📦⏳☕️💡🏗☀️🐥📦⏳…')
2121
})

elementary/src/editor/types.ts

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { Descendant, BaseEditor } from 'slate'
22
import { Element } from 'slate'
33
import { ReactEditor } from 'slate-react'
44
import { HistoryEditor } from 'slate-history'
5-
import type { NodeTextData } from 'smuggler-api'
5+
import type {
6+
NodeTextData,
7+
ChunkedDocDeprecated,
8+
DraftDocDeprecated,
9+
SlateText as RawSlateText,
10+
} from 'smuggler-api'
611

712
import lodash from 'lodash'
813

@@ -333,39 +338,74 @@ export function makeDateTime(
333338
}
334339
}
335340

341+
export function makeSlateTextFromPlainText(plaintext?: string): SlateText {
342+
return [
343+
{
344+
type: 'paragraph',
345+
children: [
346+
{
347+
text: plaintext ?? '',
348+
},
349+
],
350+
},
351+
]
352+
}
353+
354+
/**
355+
* Migrates all previous version document structre to the latest one
356+
*/
357+
function ensureCorrectNodeTextData(
358+
text: {
359+
slate?: SlateText | RawSlateText | null
360+
draft?: DraftDocDeprecated | null
361+
chunks?: ChunkedDocDeprecated | null
362+
} | null
363+
): { slate: SlateText } {
364+
let slate: SlateText
365+
if (text == null) {
366+
slate = makeSlateTextFromPlainText()
367+
} else if (text.slate != null) {
368+
slate = text.slate as SlateText
369+
} else {
370+
// There is an old style text node
371+
if (text.chunks) {
372+
slate = makeSlateTextFromPlainText(
373+
text.chunks.map((chunk) => chunk.source).join(' ')
374+
)
375+
} else if (text.draft) {
376+
slate = makeSlateTextFromPlainText(
377+
text.draft.blocks.map((block) => block.text).join('\n')
378+
)
379+
} else {
380+
slate = makeSlateTextFromPlainText(JSON.stringify(text))
381+
}
382+
}
383+
return { slate }
384+
}
385+
336386
export class TDoc {
337387
slate: SlateText
388+
chunks?: ChunkedDocDeprecated
389+
draft?: DraftDocDeprecated
338390

339-
constructor(slate: SlateText) {
340-
this.slate = slate
391+
constructor(slate: SlateText | undefined | null) {
392+
this.slate = ensureCorrectNodeTextData({ slate }).slate
341393
}
342394

343395
toNodeTextData(): NodeTextData {
344-
const { slate } = this
345-
return { slate, draft: null, chunks: null }
396+
return { ...this }
346397
}
347398

348399
static fromNodeTextData(text: NodeTextData): TDoc {
349-
const { slate } = text
350-
if (slate) {
351-
return new TDoc(slate as SlateText)
352-
}
353-
return this.makeEmpty()
400+
return new TDoc(ensureCorrectNodeTextData(text).slate)
354401
}
355402

356403
static makeEmpty(): TDoc {
357-
const text = ''
358-
const slate = [
359-
{
360-
type: kSlateBlockTypeParagraph,
361-
children: [{ text }],
362-
},
363-
]
364-
return new TDoc(slate as SlateText)
404+
return new TDoc(makeSlateTextFromPlainText())
365405
}
366406

367407
makeACopy(nid: string, isBlankCopy: boolean): TDoc {
368-
let { slate } = this
408+
let slate = this.slate
369409
const title = this.genTitle()
370410
let label
371411
if (isBlankCopy) {

smuggler-api/src/types.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { makeUrl } from './api_url'
55

66
export type SlateText = object[]
77

8-
function makeSlateFromPlainText(plaintext?: string): SlateText {
8+
/**
9+
* This hacky type unsafe method for in-file use only. For anything else please
10+
* consider using similar methods from elementary/src/editor/types.ts , those
11+
* are type safe
12+
*/
13+
export function makeSlateFromPlainText(plaintext?: string): SlateText {
914
return [
1015
{
1116
type: 'paragraph',
@@ -28,13 +33,34 @@ export function makeNodeTextData(plaintext?: string): NodeTextData {
2833

2934
export type Nid = string
3035

36+
// Types related to old document types
37+
38+
export type DocChunkDeprecated = {
39+
type: number
40+
source: string
41+
}
42+
export type ChunkedDocDeprecated = DocChunkDeprecated[]
43+
export type DraftBlockDeprecated = {
44+
data?: object
45+
depth?: number
46+
entityRanges?: any[]
47+
inlineStyleRanges?: any[]
48+
key: string
49+
text: string
50+
type?: string
51+
}
52+
export type DraftDocDeprecated = {
53+
blocks: DraftBlockDeprecated[]
54+
entityMap?: object
55+
}
56+
3157
// see smuggler/src/types.rs
3258
export type NodeTextData = {
33-
slate: SlateText | undefined
59+
slate: SlateText
3460
// Deprecated
35-
draft: any | undefined
61+
draft?: DraftDocDeprecated
3662
// Deprecated
37-
chunks: any | undefined
63+
chunks?: ChunkedDocDeprecated
3864
}
3965

4066
export enum NodeType {

truthsayer/src/external-import/BrowserHistoryImporter.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import semver from 'semver'
55
import { sleep } from 'armoury'
66
import { Spinner } from 'elementary'
77
import * as truthsayer_archaeologist_communication from 'truthsayer-archaeologist-communication'
8-
import { FromTruthsayer } from 'truthsayer-archaeologist-communication'
98
import { TruthsayerLink } from '../lib/TrueLink'
109
import BrowserLogo from '../apps-list/img/GoogleChromeLogo.svg'
1110

0 commit comments

Comments
 (0)