Skip to content

Commit dad9ec5

Browse files
committed
feat(rte): add default font family and size feature
1 parent 351dd2a commit dad9ec5

File tree

6 files changed

+62
-31
lines changed

6 files changed

+62
-31
lines changed

packages/pluggableWidgets/rich-text-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"dependencies": {
4646
"@codemirror/lang-html": "^6.4.9",
47+
"@codemirror/state": "^6.5.2",
4748
"@floating-ui/react": "^0.26.27",
4849
"@melloware/coloris": "^0.25.0",
4950
"@uiw/codemirror-theme-github": "^4.23.13",

packages/pluggableWidgets/rich-text-web/src/RichText.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@
171171
<caption>Enable spell checking</caption>
172172
<description />
173173
</property>
174+
<property key="defaultFontFamily" type="textTemplate" required="false">
175+
<caption>Default font family</caption>
176+
<description />
177+
</property>
178+
<property key="defaultFontSize" type="textTemplate" required="false">
179+
<caption>Default font size</caption>
180+
<description />
181+
</property>
174182
<property key="customFonts" type="object" isList="true" required="false">
175183
<caption>Custom fonts</caption>
176184
<description />

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function EditorWrapperInner(props: EditorWrapperProps): ReactElement {
5454
imageSource,
5555
imageSourceContent,
5656
enableDefaultUpload,
57-
formOrientation
57+
formOrientation,
58+
defaultFontFamily,
59+
defaultFontSize
5860
} = props;
5961

6062
const globalState = useContext(EditorContext);
@@ -106,6 +108,14 @@ function EditorWrapperInner(props: EditorWrapperProps): ReactElement {
106108
// eslint-disable-next-line react-hooks/exhaustive-deps
107109
}, [stringAttribute.value, calculateCounts, quillRef.current]);
108110

111+
useEffect(() => {
112+
if (quillRef.current) {
113+
(quillRef.current?.theme as MendixTheme).updateDefaultFontFamily(defaultFontFamily?.value);
114+
(quillRef.current?.theme as MendixTheme).updateDefaultFontSize(defaultFontSize?.value);
115+
}
116+
// eslint-disable-next-line react-hooks/exhaustive-deps
117+
}, [defaultFontFamily?.value, quillRef.current, defaultFontSize?.value]);
118+
109119
useEffect(() => {
110120
if (quillRef.current) {
111121
const isTransformed = updateLegacyQuillFormats(quillRef.current);

packages/pluggableWidgets/rich-text-web/src/utils/themes/mxTheme.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@ import { Context } from "quill/modules/keyboard";
66
import Picker from "quill/ui/picker";
77
import { Range } from "quill";
88

9-
const DEFAULT_FONT_SIZE = "14px";
10-
const DEFAULT_FONT_FAMILY = "helvetica";
11-
129
/**
1310
* Override quill's current theme.
1411
*/
1512
export default class MendixTheme extends SnowTheme {
1613
fontPicker?: Picker = undefined;
1714
fontSizePicker?: Picker = undefined;
1815
headerPicker?: Picker = undefined;
16+
defaultFontFamily = "helvetica";
17+
defaultFontSize = "14px";
1918
buildPickers(selects: NodeListOf<HTMLSelectElement>, icons: Record<string, string | Record<string, string>>): void {
2019
super.buildPickers(selects, icons);
2120

2221
this.pickers.forEach(picker => {
2322
const pickerLabel = picker.container.querySelector(".ql-picker-label");
2423
if (picker.container.classList.contains("ql-size")) {
2524
picker.selectItem(
26-
picker.container.querySelector(`[data-value="${DEFAULT_FONT_SIZE}"]`) as HTMLElement,
25+
picker.container.querySelector(`[data-value="${this.defaultFontFamily}"]`) as HTMLElement,
2726
false
2827
);
2928
this.fontSizePicker = picker;
3029
}
3130
if (picker.container.classList.contains("ql-font")) {
3231
picker.selectItem(
33-
picker.container.querySelector(`[data-value=${DEFAULT_FONT_FAMILY}]`) as HTMLElement,
32+
picker.container.querySelector(`[data-value=${this.defaultFontFamily}]`) as HTMLElement,
3433
false
3534
);
3635
this.fontPicker = picker;
@@ -76,9 +75,9 @@ export default class MendixTheme extends SnowTheme {
7675

7776
const format = this.quill.getFormat(currentRange.index, currentRange.length);
7877
const font = format ? (format.font as string) : undefined;
79-
this.updateFontPicker(font || DEFAULT_FONT_FAMILY);
78+
this.updateFontPicker(font || this.defaultFontFamily);
8079
const fontSize = format ? (format.size as string) : undefined;
81-
this.updateFontSizePicker(fontSize || DEFAULT_FONT_SIZE);
80+
this.updateFontSizePicker(fontSize || this.defaultFontSize);
8281
const header = format ? (format.header as string) : undefined;
8382
this.updateHeaderPicker(header);
8483
}
@@ -121,4 +120,24 @@ export default class MendixTheme extends SnowTheme {
121120
}
122121
}
123122
}
123+
124+
updateDefaultFontFamily(fontFamily?: string): void {
125+
if (fontFamily && fontFamily.trim().length > 0) {
126+
this.defaultFontFamily = fontFamily;
127+
} else {
128+
this.defaultFontFamily = "helvetica";
129+
}
130+
131+
this.updateFontPicker(this.defaultFontFamily);
132+
}
133+
134+
updateDefaultFontSize(fontSize?: string): void {
135+
if (fontSize) {
136+
this.defaultFontSize = fontSize;
137+
} else {
138+
this.defaultFontSize = "14px";
139+
}
140+
141+
this.updateFontSizePicker(this.defaultFontSize);
142+
}
124143
}

packages/pluggableWidgets/rich-text-web/typings/RichTextProps.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Mendix Widgets Framework Team
55
*/
66
import { ComponentType, ReactNode } from "react";
7-
import { ActionValue, EditableValue, ListValue } from "mendix";
7+
import { ActionValue, DynamicValue, EditableValue, ListValue } from "mendix";
88

99
export type PresetEnum = "basic" | "standard" | "full" | "custom";
1010

@@ -75,6 +75,8 @@ export interface RichTextContainerProps {
7575
onLoad?: ActionValue;
7676
onChangeType: OnChangeTypeEnum;
7777
spellCheck: boolean;
78+
defaultFontFamily?: DynamicValue<string>;
79+
defaultFontSize?: DynamicValue<string>;
7880
customFonts: CustomFontsType[];
7981
imageSource?: ListValue;
8082
imageSourceContent?: ReactNode;
@@ -122,6 +124,8 @@ export interface RichTextPreviewProps {
122124
onLoad: {} | null;
123125
onChangeType: OnChangeTypeEnum;
124126
spellCheck: boolean;
127+
defaultFontFamily: string;
128+
defaultFontSize: string;
125129
customFonts: CustomFontsPreviewType[];
126130
imageSource: {} | { caption: string } | { type: string } | null;
127131
imageSourceContent: { widgetCount: number; renderer: ComponentType<{ children: ReactNode; caption?: string }> };

pnpm-lock.yaml

Lines changed: 11 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)