Skip to content

Commit fbc30f1

Browse files
committed
Set default font size for editor
Signed-off-by: dab246 <[email protected]>
1 parent f7b76cc commit fbc30f1

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

lib/src/widgets/html_editor_widget_web.dart

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,50 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
8686
void initSummernote() async {
8787
var headString = '';
8888
var summernoteCallbacks = '''callbacks: {
89+
${widget.htmlEditorOptions.useDefaultFontSize
90+
? '''
91+
onInit: function() {
92+
const editor = document.querySelector('.note-editable');
93+
if (!editor) return;
94+
95+
editor.style.fontSize = "${widget.htmlEditorOptions.defaultFontSize}px";
96+
editor.style.lineHeight = calcLineHeightPx(`${widget.htmlEditorOptions.defaultFontSize}`) + "px";
97+
},
98+
onChange: function(contents, \$editable) {
99+
normalizeFontAndLineHeight(\$editable[0]);
100+
},
101+
onKeyup: function(e) {
102+
if (e.which === 13) {
103+
setTimeout(() => {
104+
const sel = window.getSelection();
105+
if (!sel.rangeCount) return;
106+
let node = sel.anchorNode;
107+
108+
if (node.nodeType === 3) {
109+
node = node.parentNode;
110+
}
111+
112+
if (node) {
113+
if (node.style) {
114+
node.style.removeProperty("line-height");
115+
if (node.getAttribute("style") === "") {
116+
node.removeAttribute("style");
117+
}
118+
}
119+
120+
const parent = node.parentElement;
121+
if (parent && parent.style) {
122+
parent.style.removeProperty("line-height");
123+
if (parent.getAttribute("style") === "") {
124+
parent.removeAttribute("style");
125+
}
126+
}
127+
}
128+
}, 0);
129+
}
130+
},
131+
'''
132+
: ''}
89133
onKeydown: function(e) {
90134
var chars = \$(".note-editable").text();
91135
var totalChars = chars.length;
@@ -533,7 +577,10 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
533577
${JavascriptUtils.jsHandleCreateSignature(createdViewId)}
534578
${JavascriptUtils.jsHandleOnClickSignature}
535579
${JavascriptUtils.jsDetectBrowser}
536-
${JavascriptUtils.jsHandleSetFontSize}
580+
${widget.htmlEditorOptions.useDefaultFontSize
581+
? JavascriptUtils.jsHandleSetupDefaultFontSize(widget.htmlEditorOptions.defaultFontSize, widget.htmlEditorOptions.defaultLineHeight)
582+
: ''}
583+
${JavascriptUtils.jsHandleSetFontSize(widget.htmlEditorOptions.defaultFontSize)}
537584
${JavascriptUtils.jsHandleInsertImageWithSafeSignature}
538585
539586
function onSelectionChange() {

lib/src/widgets/toolbar_widget.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,7 @@ class ToolbarWidgetState extends State<ToolbarWidget> {
870870
_actualFontSizeSelectedItem = 48;
871871
break;
872872
}
873-
widget.controller.execCommand('fontSize',
874-
argument: changed.toString());
873+
widget.controller.setFontSize(_actualFontSizeSelectedItem.toInt());
875874
updateSelectedItem(changed);
876875
}
877876
}

lib/utils/javascript_utils.dart

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ class JavascriptUtils {
187187
}
188188
''';
189189

190-
static const String jsHandleSetFontSize = '''
191-
var activeFontSize = 15;
190+
static String jsHandleSetFontSize(double defaultFontSizePx) => '''
191+
var activeFontSize = `$defaultFontSizePx`;
192+
192193
var style = document.createElement("style");
193194
document.body.appendChild(style);
194195
@@ -224,6 +225,35 @@ class JavascriptUtils {
224225
});
225226
''';
226227

228+
static String jsHandleSetupDefaultFontSize(
229+
double defaultFontSizePx,
230+
double defaultLineHeightPx,
231+
) => '''
232+
const ratio = `${defaultLineHeightPx / defaultFontSizePx}`;
233+
234+
function calcLineHeightPx(fontSize) {
235+
return Math.round(fontSize * ratio);
236+
}
237+
238+
function normalizeFontAndLineHeight(editable) {
239+
const elements = editable.querySelectorAll("[style*='font-size']");
240+
elements.forEach(element => {
241+
const fontSize = parseInt(element.style.fontSize);
242+
if (fontSize) {
243+
element.style.lineHeight = calcLineHeightPx(fontSize) + "px";
244+
}
245+
});
246+
247+
// Heading block
248+
editable.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(h => {
249+
const size = parseInt(window.getComputedStyle(h).fontSize);
250+
if (size && !h.style.lineHeight) {
251+
h.style.lineHeight = calcLineHeightPx(size) + "px";
252+
}
253+
});
254+
}
255+
''';
256+
227257
static const String jsHandleOnKeyDown = '''
228258
if (e.which === 13) { // Press "Enter"
229259
setTimeout(() => {

lib/utils/options.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class HtmlEditorOptions {
3131
this.cacheHTMLAssetOffline = false,
3232
this.normalizeHtmlTextWhenPasting = false,
3333
this.normalizeHtmlTextWhenDropping = false,
34+
this.useDefaultFontSize = true,
35+
this.defaultFontSize = 16.0,
36+
this.defaultLineHeight = 24.0,
3437
});
3538

3639
/// The editor will automatically adjust its height when the keyboard is active
@@ -148,6 +151,15 @@ class HtmlEditorOptions {
148151

149152
/// Normalize HTML text when dropping
150153
final bool normalizeHtmlTextWhenDropping;
154+
155+
/// Use default font size
156+
final bool useDefaultFontSize;
157+
158+
/// Default font size
159+
final double defaultFontSize;
160+
161+
/// Default line height
162+
final double defaultLineHeight;
151163
}
152164

153165
/// Options that modify the toolbar and its behavior

0 commit comments

Comments
 (0)