fix: rich text rendering

This commit is contained in:
Shibo Lyu 2024-09-02 01:20:01 +08:00
parent 1bbc149536
commit 29fa1988d4
4 changed files with 54 additions and 37 deletions

View file

@ -2,6 +2,7 @@
import type { BlahRichText } from '$lib/richText';
import { tw } from '$lib/tw';
import RichTextSpan from './RichTextRenderer/RichTextSpan.svelte';
import PlainTextRenderer from './RichTextRenderer/PlainTextRenderer.svelte';
export let content: BlahRichText;
let className = '';
@ -9,20 +10,14 @@
</script>
<div class={tw('rich-text', className)}>
{#each content as block}
<p>
{#each block as span}
{#if typeof span === 'string'}
{#if span === ''}
<br />
{:else}
{span}
{/if}
{:else}
{@const [text, attributes] = span}
<RichTextSpan {text} {attributes} />
{/if}
{/each}
</p>
{/each}
<p>
{#each content as span}
{#if typeof span === 'string'}
<PlainTextRenderer text={span} />
{:else}
{@const [text, attributes] = span}
<RichTextSpan {text} {attributes} />
{/if}
{/each}
</p>
</div>

View file

@ -0,0 +1,10 @@
<script lang="ts">
export let text: string;
</script>
{#each text.split('\n') as segment, idx}
{#if idx > 0}
<br />
{/if}
{segment}
{/each}

View file

@ -1,5 +1,6 @@
<script lang="ts">
import type { BlahRichTextSpanAttributes } from '$lib/richText';
import PlainTextRenderer from './PlainTextRenderer.svelte';
// From outside to inside, better align this with the RichTextInput
const renderOrder: (keyof BlahRichTextSpanAttributes)[] = [
@ -30,8 +31,10 @@
const nextAttribute = attribute ? (renderOrder[renderOrder.indexOf(attribute) + 1] ?? '') : null;
</script>
{#if attribute === '' || !attributes[attribute]}
{text}
{#if attribute === ''}
<PlainTextRenderer {text} />
{:else if !attributes[attribute]}
<svelte:self {...$$props} attribute={nextAttribute} />
{:else if attribute === 'link'}
<a href={attributes.link} target="_blank">
<svelte:self {...$$props} attribute={nextAttribute} />

View file

@ -1,3 +1,4 @@
import canonicalize from 'canonicalize';
import type { AttributeMap, Delta } from 'typewriter-editor';
import { z } from 'zod';
@ -18,10 +19,7 @@ export const blahRichTextSpanSchema = z.union([
]);
export type BlahRichTextSpan = z.input<typeof blahRichTextSpanSchema>;
export const blahRichTextBlockSchema = z.array(blahRichTextSpanSchema);
export type BlahRichTextBlock = z.input<typeof blahRichTextBlockSchema>;
export const blahRichTextSchema = z.array(blahRichTextBlockSchema);
export const blahRichTextSchema = z.array(blahRichTextSpanSchema);
export type BlahRichText = z.input<typeof blahRichTextSchema>;
function isObjectEmpty(obj: object) {
@ -48,23 +46,34 @@ function deltaAttributesToBlahRichTextSpanAttributes(
}
export function deltaToBlahRichText(delta: Delta): BlahRichText {
const blocks: BlahRichText = [];
const spans: BlahRichText = [];
let block: BlahRichTextBlock = [];
for (const op of delta.ops) {
const lines = op.insert?.split('\n');
if (!lines) continue;
const attributes = deltaAttributesToBlahRichTextSpanAttributes(op.attributes);
let lastText = '';
let lastAttributes: BlahRichTextSpanAttributes | null = null;
let canonicalizedLastAttributes: string = 'null';
const line = lines.shift();
block.push(attributes ? [line, attributes] : line);
for (const line of lines) {
blocks.push(block);
block = [];
block.push(attributes ? [line, attributes] : line);
}
function commitSpan() {
spans.push(lastAttributes === null ? lastText : [lastText, lastAttributes]);
}
return blocks;
for (const op of delta.ops) {
// Not sure in what cases op.insert would not be a string, but let's be safe
if (typeof op.insert !== 'string') continue;
const attributes = deltaAttributesToBlahRichTextSpanAttributes(op.attributes);
const canonicalizedAttributes = canonicalize(attributes) ?? 'null';
if (canonicalizedAttributes === canonicalizedLastAttributes) {
lastText += op.insert;
continue;
}
commitSpan();
lastText = op.insert;
lastAttributes = attributes;
canonicalizedLastAttributes = canonicalizedAttributes;
}
commitSpan();
return spans;
}