Skip to content

Commit

Permalink
More bundle size improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed May 8, 2024
1 parent 9be94b3 commit 4b854b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
40 changes: 5 additions & 35 deletions packages/keystatic/src/form/fields/document/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { format, parse as markdocParse } from '#markdoc';

import type { Descendant } from 'slate';
import { parse } from '#markdoc';

import { fromMarkdoc } from './markdoc/from-markdoc';
import { toMarkdocDocument } from './markdoc/to-markdoc';
import { DocumentFeatures } from './DocumentEditor/document-features';
import { deserializeFiles } from './DocumentEditor/component-blocks/document-field';
import { collectDirectoriesUsedInSchema } from '../../../app/tree-key';
Expand All @@ -19,13 +16,13 @@ import { text } from '../text';
import {
DocumentFieldInput,
normalizeDocumentFieldChildren,
serializeMarkdoc,
} from '#field-ui/document';
import { object } from '../object';
import { FieldDataError } from '../error';
import { basicFormFieldWithSimpleReaderParse } from '../utils';
import { fixPath } from '../../../app/path-utils';

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

type HeadingLevels = true | readonly (1 | 2 | 3 | 4 | 5 | 6)[];
Expand Down Expand Up @@ -286,7 +283,7 @@ export function document({

parse(_, data) {
const markdoc = textDecoder.decode(data.content);
const document = fromMarkdoc(markdocParse(markdoc), componentBlocks);
const document = fromMarkdoc(parse(markdoc), componentBlocks);
return deserializeFiles(
normalizeDocumentFieldChildren(
documentFeatures,
Expand Down Expand Up @@ -322,39 +319,12 @@ export function document({
: []),
],
serialize(value, opts) {
const { extraFiles, node } = toMarkdocDocument(
value as any as Descendant[],
{
componentBlocks,
documentFeatures,
slug: opts.slug,
}
);

const other = new Map<string, Uint8Array>();
const external = new Map<string, Map<string, Uint8Array>>();
for (const file of extraFiles) {
if (file.parent === undefined) {
other.set(file.path, file.contents);
continue;
}
if (!external.has(file.parent)) {
external.set(file.parent, new Map());
}
external.get(file.parent)!.set(file.path, file.contents);
}

return {
content: textEncoder.encode(format(markdocParse(format(node)))),
other,
external,
value: undefined,
};
return serializeMarkdoc(value, opts, componentBlocks, documentFeatures);
},
reader: {
parse(value, data) {
const markdoc = textDecoder.decode(data.content);
const document = fromMarkdoc(markdocParse(markdoc), componentBlocks);
const document = fromMarkdoc(parse(markdoc), componentBlocks);
return deserializeFiles(
document,
componentBlocks,
Expand Down
37 changes: 37 additions & 0 deletions packages/keystatic/src/form/fields/document/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,50 @@ import { DocumentEditor, Editor } from './DocumentEditor';
import { DocumentFeatures } from './DocumentEditor/document-features';
import { createDocumentEditorForNormalization } from './DocumentEditor/create-editor';
import type { Descendant } from 'slate';
import { format, parse } from '#markdoc';
import { toMarkdocDocument } from './markdoc/to-markdoc';

let i = 0;

function newKey() {
return i++;
}

const encoder = new TextEncoder();

export function serializeMarkdoc(
value: DocumentElement[],
opts: { slug: string | undefined },
componentBlocks: Record<string, ComponentBlock>,
documentFeatures: DocumentFeatures
) {
const { extraFiles, node } = toMarkdocDocument(value as any as Descendant[], {
componentBlocks,
documentFeatures,
slug: opts.slug,
});

const other = new Map<string, Uint8Array>();
const external = new Map<string, Map<string, Uint8Array>>();
for (const file of extraFiles) {
if (file.parent === undefined) {
other.set(file.path, file.contents);
continue;
}
if (!external.has(file.parent)) {
external.set(file.parent, new Map());
}
external.get(file.parent)!.set(file.path, file.contents);
}

return {
content: encoder.encode(format(parse(format(node)))),
other,
external,
value: undefined,
};
}

export function normalizeDocumentFieldChildren(
documentFeatures: DocumentFeatures,
componentBlocks: Record<string, ComponentBlock>,
Expand Down
3 changes: 2 additions & 1 deletion packages/keystatic/src/form/fields/empty-field-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export let SlugFieldInput = empty,
createEditorStateFromYJS = empty,
prosemirrorToYXmlFragment = empty,
normalizeDocumentFieldChildren = empty,
slugify = empty;
slugify = empty,
serializeMarkdoc = empty;

0 comments on commit 4b854b8

Please sign in to comment.