Skip to content

Commit

Permalink
feat: Avoid rendering or exporting empty text content
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanokomiya committed Feb 9, 2024
1 parent ced273a commit 5bb7aab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/composables/shapeRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocOutput } from "../models/document";
import { getShapeTextBounds } from "../shapes";
import { getDocCompositionInfo, renderDocByComposition } from "../utils/textEditor";
import { getDocCompositionInfo, hasDocNoContent, renderDocByComposition } from "../utils/textEditor";
import { walkTree } from "../utils/tree";
import { ImageStore } from "./imageStore";
import { ShapeComposite } from "./shapeComposite";
Expand All @@ -23,7 +23,7 @@ export function newShapeRenderer(option: Option) {
option.shapeComposite.render(ctx, shape, option.imageStore);

const doc = docMap[shape.id];
if (doc && !ignoreDocIdSet.has(shape.id)) {
if (doc && !ignoreDocIdSet.has(shape.id) && !hasDocNoContent(doc)) {
ctx.save();
const bounds = getShapeTextBounds(option.shapeComposite.getShapeStruct, shape);
ctx.transform(...bounds.affine);
Expand Down
4 changes: 2 additions & 2 deletions src/composables/shapeSVGRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocOutput } from "../models/document";
import { getShapeTextBounds } from "../shapes";
import { createSVGElement, createSVGSVGElement, renderTransform } from "../utils/svgElements";
import { getDocCompositionInfo, renderSVGDocByComposition } from "../utils/textEditor";
import { getDocCompositionInfo, hasDocNoContent, renderSVGDocByComposition } from "../utils/textEditor";
import { walkTree } from "../utils/tree";
import { ImageStore } from "./imageStore";
import { ShapeComposite } from "./shapeComposite";
Expand All @@ -25,7 +25,7 @@ export function newShapeSVGRenderer(option: Option) {
if (!shapeElmInfo) return;

const doc = docMap[shape.id];
if (!doc) {
if (!doc || hasDocNoContent(doc)) {
const shapeElm = createSVGElement(shapeElmInfo.tag, shapeElmInfo.attributes, shapeElmInfo.children);
root.appendChild(shapeElm);
return;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/textEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getOutputSelection,
getRawCursor,
getWordRangeAtCursor,
hasDocNoContent,
isCursorInDoc,
isLinebreak,
isUrlText,
Expand Down Expand Up @@ -89,6 +90,17 @@ describe("getDocLength", () => {
});
});

describe("hasDocNoContent", () => {
test("should return true when doc has no content", () => {
expect(hasDocNoContent([])).toBe(true);
expect(hasDocNoContent([{ insert: "\n" }])).toBe(true);
expect(hasDocNoContent([{ insert: " " }])).toBe(false);
expect(hasDocNoContent([{ insert: "a" }])).toBe(false);
expect(hasDocNoContent([{ insert: "a" }, { insert: "b" }])).toBe(false);
expect(hasDocNoContent([{ insert: "\n" }, { insert: "\n" }])).toBe(false);
});
});

describe("getDocRawLength", () => {
test("should return doc length based on doc delta", () => {
expect(getDocRawLength([{ insert: "a" }, { insert: "b" }, { insert: "c" }])).toBe(3);
Expand Down
9 changes: 9 additions & 0 deletions src/utils/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ export function getDocLength(doc: DocOutput): number {
return doc.map((d) => splitToSegments(d.insert)).reduce((p, v) => p + v.length, 0);
}

/**
* Returns true when doc has no content.
* i.e. There's no items or only one line break.
*/
export function hasDocNoContent(doc: DocOutput): boolean {
if (doc.length === 0) return true;
return doc.length === 1 && doc[0].insert.length === 1 && isLinebreak(doc[0].insert);
}

/**
* Returns doc length based on doc delta
*/
Expand Down

0 comments on commit 5bb7aab

Please sign in to comment.