fix(core): load fonts from selfhost url (#5789)

This commit is contained in:
LongYinan
2024-02-02 14:41:06 +00:00
parent 2ed30a0c2e
commit 899528dcfd

View File

@@ -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 => {