Skip to content

Commit

Permalink
refactor: optimize word query
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind committed Jan 16, 2025
1 parent 9af51f0 commit 474dd10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions blocksuite/playground/examples/renderer/canvas.worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { type ParagraphData } from './types.js';

function segmentWords(text: string): string[] {
const segmenter = new Intl.Segmenter(void 0, {
granularity: 'word',
});
return Array.from(segmenter.segment(text)).map(({ segment }) => segment);
}

class CanvasWorkerManager {
private canvas: OffscreenCanvas | null = null;
private ctx: OffscreenCanvasRenderingContext2D | null = null;
Expand All @@ -17,8 +24,9 @@ class CanvasWorkerManager {
if (!canvas || !ctx) return;

ctx.fillStyle = 'yellow';
paragraphs.forEach(({ rect, style }) => {
console.log(JSON.stringify(rect), style.fontSize);
paragraphs.forEach(({ rect, text }) => {
const words = segmentWords(text);
console.log(words);
const x = rect.left - editorRect.left;
const y = rect.top - editorRect.top;
ctx.fillRect(x, y, rect.width, rect.height);
Expand Down
4 changes: 3 additions & 1 deletion blocksuite/playground/examples/renderer/to-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ function initWorker(width: number, height: number) {
}

function getWorkerData(editorHost: EditorHost) {
const paragraphBlocks = editorHost.querySelectorAll('affine-paragraph');
const paragraphBlocks = editorHost.querySelectorAll(
'.affine-paragraph-rich-text-wrapper [data-v-text="true"]'
);
const paragraphs: ParagraphData[] = Array.from(paragraphBlocks).map(p => {
const rect = p.getBoundingClientRect();
const computedStyle = window.getComputedStyle(p);
Expand Down

0 comments on commit 474dd10

Please sign in to comment.