From 899528dcfddcf9890272b2103056c6e53d73c226 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 2 Feb 2024 14:41:06 +0000 Subject: [PATCH] fix(core): load fonts from selfhost url (#5789) --- .../blocksuite/block-suite-editor/specs.ts | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts index ab2eb8b2e7..0c9f49c4a7 100644 --- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts +++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts @@ -1,9 +1,10 @@ -import type { BlockSpec } from '@blocksuite/block-std'; +import type { BlockServiceOptions, BlockSpec } from '@blocksuite/block-std'; import type { ParagraphService } from '@blocksuite/blocks'; import { AttachmentService, DocEditorBlockSpecs, EdgelessEditorBlockSpecs, + PageService, } from '@blocksuite/blocks'; import bytes from 'bytes'; import { html, unsafeStatic } from 'lit/static-html.js'; @@ -17,6 +18,26 @@ class CustomAttachmentService extends AttachmentService { } } +class CustomPageService extends PageService { + constructor(opt: BlockServiceOptions) { + super(opt); + const officialDomains = new Set(['affine.pro', 'affine.fail']); + const load = this.fontLoader.load.bind(this.fontLoader); + this.fontLoader.load = function (fonts) { + if (!officialDomains.has(window.location.host)) { + return load( + fonts.map(f => ({ + ...f, + // self-hosted fonts are served from /assets + url: '/assets' + new URL(f.url).pathname.split('/').pop(), + })) + ); + } + return load(fonts); + }; + } +} + type AffineReference = HTMLElementTagNameMap['affine-reference']; type PageReferenceRenderer = (reference: AffineReference) => React.ReactElement; @@ -76,6 +97,12 @@ export const docModeSpecs = DocEditorBlockSpecs.map(spec => { service: CustomAttachmentService, }; } + if (spec.schema.model.flavour === 'affine:page') { + return { + ...spec, + service: CustomPageService, + }; + } return spec; }); export const edgelessModeSpecs = EdgelessEditorBlockSpecs.map(spec => {