Skip to content

Commit 13ba849

Browse files
feat(rich-tex-web): integrate custom fonts support in Editor and Toolbar components
1 parent f4218cc commit 13ba849

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

packages/pluggableWidgets/rich-text-web/src/components/Editor.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "react";
1414
import QuillTableBetter from "../utils/formats/quill-table-better/quill-table-better";
1515
import "../utils/formats/quill-table-better/assets/css/quill-table-better.scss";
16+
import { FontStyleAttributor, formatFonts } from "../utils/formats/fonts";
1617
import "../utils/customPluginRegisters";
1718
import MxQuill from "../utils/MxQuill";
1819
import {
@@ -28,8 +29,10 @@ import { RESIZE_MODULE_CONFIG } from "../utils/formats/resizeModuleConfig";
2829
import { ACTION_DISPATCHER } from "../utils/helpers";
2930
import { EditorDispatchContext } from "../store/EditorProvider";
3031
import { SET_FULLSCREEN_ACTION } from "../store/store";
32+
import { CustomFontsType } from "typings/RichTextProps";
3133

3234
export interface EditorProps {
35+
customFonts: CustomFontsType[];
3336
defaultValue?: string;
3437
onTextChange?: (...args: [delta: Delta, oldContent: Delta, source: EmitterSource]) => void;
3538
onSelectionChange?: (...args: [range: Range, oldRange: Range, source: EmitterSource]) => void;
@@ -42,6 +45,9 @@ export interface EditorProps {
4245

4346
// Editor is an uncontrolled React component
4447
const Editor = forwardRef((props: EditorProps, ref: MutableRefObject<Quill | null>) => {
48+
const fonts = formatFonts(props.customFonts);
49+
const FontStyle = new FontStyleAttributor(fonts);
50+
Quill.register(FontStyle, true);
4551
const { theme, defaultValue, style, className, toolbarId, onTextChange, onSelectionChange, readOnly } = props;
4652
const containerRef = useRef<HTMLDivElement>(null);
4753
const modalRef = useRef<HTMLDivElement>(null);

packages/pluggableWidgets/rich-text-web/src/components/EditorWrapper.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ function EditorWrapperInner(props: EditorWrapperProps): ReactElement {
208208
className={"widget-rich-text-container"}
209209
readOnly={stringAttribute.readOnly}
210210
key={`${toolbarId}_${stringAttribute.readOnly}`}
211+
customFonts={props.customFonts}
211212
/>
212213
</div>
213214
{enableStatusBar && (

packages/pluggableWidgets/rich-text-web/src/components/Toolbar.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CSSProperties, ReactElement, RefObject, createElement, forwardRef } fro
44
import { CustomFontsType, PresetEnum } from "typings/RichTextProps";
55
import { FormatsContainer, ToolbarContext, presetToNumberConverter } from "./CustomToolbars/ToolbarWrapper";
66
import { TOOLBAR_MAPPING, toolbarContentType } from "./CustomToolbars/constants";
7+
import { formatFonts } from "src/utils/formats/fonts";
78

89
export interface ToolbarProps {
910
customFonts?: CustomFontsType[];
@@ -72,11 +73,7 @@ const Toolbar = forwardRef((props: ToolbarProps, ref: RefObject<HTMLDivElement>)
7273
type FontListType = Array<{ value: string; description: string; style: string }>;
7374
value = [
7475
...(currentToolbar.value as FontListType),
75-
...(props.customFonts ?? []).map((font: CustomFontsType) => ({
76-
value: font.fontName?.value?.toLowerCase().split(" ").join("-"),
77-
description: font.fontName?.value,
78-
style: `'${font.fontName?.value}'`
79-
}))
76+
...formatFonts(props.customFonts ?? [])
8077
].sort((a, b) => (a.value ?? "").localeCompare(b.value ?? ""));
8178
}
8279

packages/pluggableWidgets/rich-text-web/src/utils/formats/fonts.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Scope, StyleAttributor } from "parchment";
2-
import Quill from "quill";
32
import "./fonts.scss";
3+
import { CustomFontsType } from "typings/RichTextProps";
44

55
export const FONT_LIST = [
66
{ value: "andale-mono", description: "Andale Mono", style: "'andale mono', monospace" },
@@ -22,13 +22,21 @@ const config = {
2222
scope: Scope.INLINE
2323
};
2424

25-
class FontStyleAttributor extends StyleAttributor {
25+
export class FontStyleAttributor extends StyleAttributor {
26+
private fontList: typeof FONT_LIST = [];
27+
28+
constructor(fontList: typeof FONT_LIST) {
29+
super("font", "font-family", config);
30+
this.fontList = fontList;
31+
}
32+
2633
add(node: HTMLElement, value: any): boolean {
2734
if (!this.canAdd(node, value)) {
2835
return false;
2936
}
3037
node.dataset.value = value;
31-
const style = FONT_LIST.find(x => x.value === value)?.style;
38+
const allFonts = [...FONT_LIST, ...this.fontList];
39+
const style = allFonts.find(x => x.value === value)?.style;
3240
if (style) {
3341
super.add(node, style);
3442
} else {
@@ -46,6 +54,11 @@ class FontStyleAttributor extends StyleAttributor {
4654
}
4755
}
4856

49-
const FontStyle = new FontStyleAttributor("font", "font-family", config);
50-
51-
Quill.register(FontStyle, true);
57+
export function formatFonts(fonts: CustomFontsType[] = []): typeof FONT_LIST {
58+
console.info("formatFonts", { fonts });
59+
return fonts.map(font => ({
60+
value: font.fontName?.value?.toLowerCase().split(" ").join("-") ?? "",
61+
description: font.fontName?.value ?? "",
62+
style: `'${font.fontName?.value}'`
63+
}));
64+
}

0 commit comments

Comments
 (0)