Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1477f83
feat(editor): add Container EmailNode extension
gabrielmfern Mar 30, 2026
950c444
refactor(editor): delegate Container rendering to the extension
gabrielmfern Mar 30, 2026
d549707
lint
gabrielmfern Mar 30, 2026
997952f
use inline snapshots
gabrielmfern Mar 30, 2026
7cd127f
remove obsolete
gabrielmfern Mar 30, 2026
f99e44a
ensure global content is transparent
gabrielmfern Mar 31, 2026
66839b3
custom trailing node that works well with Container
gabrielmfern Mar 31, 2026
4d59260
trailing node tests
gabrielmfern Mar 31, 2026
8360217
paragraph as default trailing node
gabrielmfern Mar 31, 2026
89e882d
tweak tests a bit
gabrielmfern Mar 31, 2026
2ee5a5e
move tests out of trailing node
gabrielmfern Mar 31, 2026
4f300ab
wip debugging
gabrielmfern Mar 31, 2026
3f87c92
paleative fixes 🥲
gabrielmfern Apr 1, 2026
a9bef20
cleanup tests
gabrielmfern Apr 1, 2026
afea28e
add liveblocks-simulating tests
gabrielmfern Apr 1, 2026
cb8155f
lint
gabrielmfern Apr 1, 2026
3eb95d2
only apply to doc changing transactions, not ignoring y-sync ones
gabrielmfern Apr 1, 2026
fb0e3b7
use newState.eq(oldState) to make sure it wraps existing collaborativ…
gabrielmfern Apr 1, 2026
1c2cbae
fix tests
gabrielmfern Apr 1, 2026
21fc9d5
add some comments
gabrielmfern Apr 1, 2026
36912b0
update tests, skip failnig one that we basically can't fix
gabrielmfern Apr 1, 2026
1146f0e
improve comments
gabrielmfern Apr 1, 2026
b34853d
Merge branch 'canary' into feat/container-extension
gabrielmfern Apr 1, 2026
f1e60a8
address cubic's comment
gabrielmfern Apr 1, 2026
2e65914
lint
gabrielmfern Apr 1, 2026
c8c24f9
use actual nodes in the test
gabrielmfern Apr 1, 2026
ffb3b92
check for empty through textContent
gabrielmfern Apr 1, 2026
e55970b
break lines for the comments
gabrielmfern Apr 1, 2026
2f8d618
Revert "check for empty through textContent"
gabrielmfern Apr 1, 2026
e36300e
just an image is not a visually empty document
gabrielmfern Apr 1, 2026
4865706
take containers into account for is document visually empty
gabrielmfern Apr 1, 2026
699c7f4
update tests to test for containers too
gabrielmfern Apr 1, 2026
68fc808
make container also match table structure of the React Email containe…
gabrielmfern Apr 1, 2026
e84a9df
match exact structure
gabrielmfern Apr 1, 2026
2bbf886
add test to ensure parsing of container
gabrielmfern Apr 1, 2026
9fcd11b
lint
gabrielmfern Apr 1, 2026
76519d3
add test to ensure document is not visually empty with white spaces
gabrielmfern Apr 1, 2026
e016db3
don't check for childCount in paragraph
gabrielmfern Apr 1, 2026
e45b992
bump
gabrielmfern Apr 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-email/editor",
"version": "0.0.0-experimental.39",
"version": "0.0.0-experimental.40",
"description": "",
"sideEffects": [
"**/*.css"
Expand Down
159 changes: 102 additions & 57 deletions packages/editor/src/core/is-document-visually-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,121 @@
import type { Node } from '@tiptap/pm/model';
import { Schema } from '@tiptap/pm/model';
import { describe, expect, it } from 'vitest';
import { isDocumentVisuallyEmpty } from './is-document-visually-empty';

type MockDoc = {
childCount: number;
child: (index: number) => {
type: { name: string };
textContent: string;
content: { childCount: number };
};
};

function createDoc(
nodes: Array<{ type: string; textContent?: string; childCount?: number }>,
): Node {
const doc: MockDoc = {
childCount: nodes.length,
child: (index) => ({
type: { name: nodes[index]!.type },
textContent: nodes[index]!.textContent ?? '',
content: { childCount: nodes[index]!.childCount ?? 0 },
}),
};

return doc as unknown as Node;
}
const schema = new Schema({
nodes: {
doc: { content: 'block+' },
paragraph: { group: 'block', content: 'inline*' },
text: { group: 'inline' },
globalContent: { group: 'block', atom: true },
container: { group: 'block', content: 'block+' },
image: { group: 'block', atom: true },
},
});

describe('isDocumentVisuallyEmpty', () => {
it('returns true when document only contains global content', () => {
const doc = createDoc([{ type: 'globalContent' }]);
describe('without container', () => {
it('returns true when document only contains global content', () => {
const doc = schema.node('doc', null, [schema.node('globalContent')]);

expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});
expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});

it('returns true when document contains global content and one empty paragraph', () => {
const doc = createDoc([
{ type: 'globalContent' },
{ type: 'paragraph', textContent: ' ', childCount: 0 },
]);
it('returns true when document contains global content and one empty paragraph', () => {
const doc = schema.node('doc', null, [
schema.node('globalContent'),
schema.node('paragraph'),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});
expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});

it('returns false when document contains one empty paragraph with inline content', () => {
const doc = createDoc([
{ type: 'paragraph', textContent: ' ', childCount: 1 },
]);
it('returns false when paragraph contains whitespace text', () => {
const doc = schema.node('doc', null, [
schema.node('paragraph', null, [schema.text(' ')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});
expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when document contains multiple empty paragraphs', () => {
const doc = createDoc([
{ type: 'paragraph', textContent: '', childCount: 0 },
{ type: 'paragraph', textContent: ' ', childCount: 0 },
]);
it('returns false when document contains multiple empty paragraphs', () => {
const doc = schema.node('doc', null, [
schema.node('paragraph'),
schema.node('paragraph'),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});
expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when document contains only an image node', () => {
const doc = schema.node('doc', null, [schema.node('image')]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when document contains both an image and text', () => {
const doc = schema.node('doc', null, [
schema.node('image'),
schema.node('paragraph', null, [schema.text('hello world')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when document contains only an image node', () => {
const doc = createDoc([{ type: 'image' }]);
it('considers just white spaces as not empty', () => {
const doc = schema.node('doc', null, [
schema.node('paragraph', null, [schema.text(' ')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});
});

it('returns false when document contains both an image and pasted text', () => {
const doc = createDoc([
{ type: 'image' },
{ type: 'paragraph', textContent: 'hello world' },
]);
describe('with container', () => {
it('returns true when container holds one empty paragraph', () => {
const doc = schema.node('doc', null, [
schema.node('container', null, [schema.node('paragraph')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});

it('returns true when global content precedes a container with one empty paragraph', () => {
const doc = schema.node('doc', null, [
schema.node('globalContent'),
schema.node('container', null, [schema.node('paragraph')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(true);
});

it('returns false when container holds a paragraph with text', () => {
const doc = schema.node('doc', null, [
schema.node('container', null, [
schema.node('paragraph', null, [schema.text('hello')]),
]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when container holds multiple empty paragraphs', () => {
const doc = schema.node('doc', null, [
schema.node('container', null, [
schema.node('paragraph'),
schema.node('paragraph'),
]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});

it('returns false when container holds an image node', () => {
const doc = schema.node('doc', null, [
schema.node('container', null, [schema.node('image')]),
]);

expect(isDocumentVisuallyEmpty(doc)).toBe(false);
expect(isDocumentVisuallyEmpty(doc)).toBe(false);
});
});
});
38 changes: 23 additions & 15 deletions packages/editor/src/core/is-document-visually-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import type { Node } from '@tiptap/pm/model';

export function isDocumentVisuallyEmpty(doc: Node): boolean {
let nonGlobalNodeCount = 0;
let firstNonGlobalNode: {
type: { name: string };
textContent: string;
childCount: number;
} | null = null;
let firstNonGlobalNode: Node | null = null;

for (let index = 0; index < doc.childCount; index += 1) {
const node = doc.child(index);
Expand All @@ -18,11 +14,7 @@ export function isDocumentVisuallyEmpty(doc: Node): boolean {
nonGlobalNodeCount += 1;

if (firstNonGlobalNode === null) {
firstNonGlobalNode = {
type: node.type,
textContent: node.textContent,
childCount: node.content.childCount,
};
firstNonGlobalNode = node;
}
}

Expand All @@ -34,9 +26,25 @@ export function isDocumentVisuallyEmpty(doc: Node): boolean {
return false;
}

return (
firstNonGlobalNode?.type.name === 'paragraph' &&
firstNonGlobalNode.textContent.trim().length === 0 &&
firstNonGlobalNode.childCount === 0
);
if (firstNonGlobalNode!.type.name === 'container') {
return hasOnlyEmptyParagraph(firstNonGlobalNode!);
}

return isEmptyParagraph(firstNonGlobalNode!);
}

function hasOnlyEmptyParagraph(node: Node): boolean {
if (node.childCount === 0) {
return true;
}

if (node.childCount !== 1) {
return false;
}

return isEmptyParagraph(node.child(0));
}

function isEmptyParagraph(node: Node): boolean {
return node.type.name === 'paragraph' && node.textContent.length === 0;
}
14 changes: 2 additions & 12 deletions packages/editor/src/core/serializer/default-base-template.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Head, Html, Preview, Section } from '@react-email/components';
import { Body, Head, Html, Preview } from '@react-email/components';
import type * as React from 'react';

type BaseTemplateProps = {
Expand All @@ -23,17 +23,7 @@ export function DefaultBaseTemplate({
</Head>
{previewText && previewText !== '' && <Preview>{previewText}</Preview>}

<Body>
<Section width="100%" align="center">
<Section
style={{
width: '100%',
}}
>
{children}
</Section>
</Section>
</Body>
<Body>{children}</Body>
</Html>
);
}
10 changes: 1 addition & 9 deletions packages/editor/src/core/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@tiptap/react';
import * as React from 'react';
import { StarterKit } from '../extensions';
import { hasCollaborationExtension } from '../utils/is-collaboration';
import { createDropHandler } from './create-drop-handler';
import {
createPasteHandler,
Expand All @@ -15,15 +16,6 @@ import {
} from './create-paste-handler';
import { isDocumentVisuallyEmpty } from './is-document-visually-empty';

const COLLABORATION_EXTENSION_NAMES = new Set([
'liveblocksExtension',
'collaboration',
]);

function hasCollaborationExtension(exts: Extensions): boolean {
return exts.some((ext) => COLLABORATION_EXTENSION_NAMES.has(ext.name));
}

type Merge<A, B> = A & Omit<B, keyof A>;

export function useEditor({
Expand Down
Loading
Loading