Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support set font size text use pixel unit #25

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/html_editor_controller_unsupported.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class HtmlEditorController {
/// A function to quickly call a Summernote API function in a readable format
void execSummernoteAPI(String nameAPI, {String? value}) {}

/// A function to set font size for text selected
void setFontSize(int size) {}

/// A function to execute JS passed as a [WebScript] to the editor. This should
/// only be used on Flutter Web.
Future<dynamic> evaluateJavascriptWeb(String name,
Expand Down
9 changes: 9 additions & 0 deletions lib/src/html_editor_controller_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ class HtmlEditorController extends unsupported.HtmlEditorController {
});
}

/// A function to set font size for text selected
@override
void setFontSize(int size) {
_evaluateJavascriptWeb(data: {
'type': 'toIframe: setFontSize',
'size': size
});
}

/// A function to execute JS passed as a [WebScript] to the editor. This should
/// only be used on Flutter Web.
@override
Expand Down
22 changes: 21 additions & 1 deletion lib/src/widgets/html_editor_widget_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
const nodeDropZone = document.querySelector('.note-editor > .note-dropzone');
if (nodeDropZone) {
nodeDropZone.setAttribute('style', styleDropZone);
console.log("nodeDropZone: " + nodeDropZone.outerHTML);
}
}
if (data["type"].includes("setText")) {
Expand Down Expand Up @@ -384,6 +383,9 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
\$('#summernote-2').summernote(nameAPI, value);
}
}
if (data["type"].includes("setFontSize")) {
setFontSize(data["size"]);
}
if (data["type"].includes("changeListStyle")) {
var \$focusNode = \$(window.getSelection().focusNode);
var \$parentList = \$focusNode.closest("div.note-editable ol, div.note-editable ul");
Expand Down Expand Up @@ -471,6 +473,7 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {

${JavascriptUtils.jsHandleOnClickSignature}
${JavascriptUtils.jsDetectBrowser}
${JavascriptUtils.jsHandleSetFontSize}

function onSelectionChange() {
let {anchorNode, anchorOffset, focusNode, focusOffset} = document.getSelection();
Expand Down Expand Up @@ -801,6 +804,20 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
});\n
""";
}
if (c.onTextFontSizeChanged != null) {
callbacks =
"""$callbacks \$('#summernote-2').on('summernote.mouseup', function(_) {
try {
var fontSize = \$(window.getSelection().getRangeAt(0).startContainer.parentNode).css("font-size")
fontSize = fontSize.replace("px", "");
var size = parseInt(fontSize);
window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: onTextFontSizeChanged", "size": size}), "*");
} catch(e) {
console.log("JavascriptUtils::summernote.mouseup::Exception", e);
}
});\n
""";
}
return callbacks;
}

Expand Down Expand Up @@ -893,6 +910,9 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
if (data['type'].contains('characterCount')) {
widget.controller.characterCount = data['totalChars'];
}
if (data['type'].contains('onTextFontSizeChanged')) {
c.onTextFontSizeChanged!.call(data['size']);
}
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Callbacks {
this.onNavigationRequestMobile,
this.onPaste,
this.onScroll,
this.onTextFontSizeChanged,
});

/// Called before certain commands are fired and the editor is in rich text view.
Expand Down Expand Up @@ -184,4 +185,7 @@ class Callbacks {
/// Note: This function will be repeatedly called while the editor is scrolled.
/// Make sure to factor that into your implementation.
void Function()? onScroll;

/// Called whenever the mouse/finger is released and the editor is in rich text view.
void Function(int?)? onTextFontSizeChanged;
}
33 changes: 33 additions & 0 deletions lib/utils/javascript_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,37 @@ class JavascriptUtils {
}
}
''';

static const String jsHandleSetFontSize = '''
var activeFontSize = 15;
var style = document.createElement("style");
document.body.appendChild(style);

function setFontSize(value) {
\$('#summernote-2').summernote('focus');
document.execCommand("fontSize", false, 20);
activeFontSize = value;
createStyle();
updateTags();
}

function updateTags() {
var nodeEditor = document.querySelector('.note-editable');
var fontElements = nodeEditor.getElementsByTagName("font");
for (var i = 0, len = fontElements.length; i < len; ++i) {
if (fontElements[i].size == "7") {
fontElements[i].removeAttribute("size");
fontElements[i].style.fontSize = activeFontSize + "px";
}
}
}

function createStyle() {
style.innerHTML = '.note-editable font[size="7"]{font-size: ' + activeFontSize + 'px}';
}

\$('#summernote-2').on('summernote.keyup', function(_, e) {
updateTags();
});
''';
}