feat: added editor settings

This commit is contained in:
jideguru
2021-05-31 22:19:49 +01:00
parent 3e38746c0c
commit 666bd5f58a
9 changed files with 461 additions and 131 deletions

View File

@@ -8,6 +8,7 @@ import 'package:webview_flutter/webview_flutter.dart';
import '../models/command_state.dart';
/// A class that handles all editor-related javascript functions
class JavascriptExecutorBase {
WebViewController? _controller;
@@ -193,6 +194,41 @@ class JavascriptExecutorBase {
await executeJavascript("disableImageResizing()");
}
// Editor settings commands
focus() async {
await executeJavascript("focus()");
}
unFocus() async {
await executeJavascript("blurFocus()");
}
setBackgroundColor(Color? color) async {
String? hex = color!.toHexColorString();
await executeJavascript("setBackgroundColor('$hex')");
}
setBackgroundImage(String image) async {
await executeJavascript("setBackgroundImage('$image')");
}
setBaseTextColor(Color? color) async {
String? hex = color!.toHexColorString();
await executeJavascript("setBaseTextColor('$hex')");
}
setPadding(EdgeInsets? padding) async {
String left = padding!.left.toString();
String top = padding.top.toString();
String right = padding.right.toString();
String bottom = padding.bottom.toString();
await executeJavascript("setPadding('$left', '$top', '$right', '$bottom')");
}
setPlaceholder(String placeholder) async {
await executeJavascript("setPlaceholder('$placeholder')");
}
static decodeHtml(String html) {
return Uri.decodeFull(html);
}
@@ -201,87 +237,89 @@ class JavascriptExecutorBase {
return Uri.encodeFull(html);
}
// bool shouldOverrideUrlLoading(String url) {
// String decodedUrl;
// try {
// decodedUrl = decodeHtml(url);
// } catch (e) {
// // No handling
// return false;
// }
//
// if (url.indexOf(editorStateChangedCallbackScheme) == 0) {
// editorStateChanged(
// decodedUrl.substring(editorStateChangedCallbackScheme.length));
// return true;
// }
//
// return false;
// }
//
// editorStateChanged(String statesString) {
// try {
// var editorState = EditorState.fromJson(jsonDecode(statesString));
//
// bool currentHtmlChanged = this.htmlField != editorState.html;
// this.htmlField = editorState.html;
//
// retrievedEditorState(editorState.didHtmlChange, editorState.commandStates)
//
// if (currentHtmlChanged) {
// fireHtmlChangedListenersAsync(editorState.html);
// }
// }
// catch (e) {
// throw("Could not parse command states: $statesString $e");
// }
// }
//
// retrievedEditorState(bool didHtmlChange,
// Map<CommandName, CommandState> commandStates) {
// if (this.didHtmlChange != didHtmlChange) {
// this.didHtmlChange = didHtmlChange;
// didHtmlChangeListeners.forEach {
// it.didHtmlChange(didHtmlChange);
// }
// }
//
// handleRetrievedCommandStates(commandStates)
// }
//
// handleRetrievedCommandStates(Map<CommandName, CommandState> commandStates) {
// determineDerivedCommandStates(commandStates)
//
// this.commandStates = commandStates;
//
// commandStatesChangedListeners.forEach {
// it.invoke(this.commandStates)
// }
// }
bool shouldOverrideUrlLoading(String url) {
String decodedUrl;
try {
decodedUrl = decodeHtml(url);
} catch (e) {
// No handling
return false;
}
// determineDerivedCommandStates(Map<CommandName, CommandState> commandStates) {
// commandStates[CommandName.FORMATBLOCK]?.let { formatCommandState ->
// commandStates.put(CommandName.H1, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h1")))
// commandStates.put(CommandName.H2, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h2")))
// commandStates.put(CommandName.H3, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h3")))
// commandStates.put(CommandName.H4, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h4")))
// commandStates.put(CommandName.H5, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h5")))
// commandStates.put(CommandName.H6, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h6")))
// commandStates.put(CommandName.P, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "p")))
// commandStates.put(CommandName.PRE, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "pre")))
// commandStates.put(CommandName.BR, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "")))
// commandStates.put(CommandName.BLOCKQUOTE, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "blockquote")))
// }
//
// commandStates[CommandName.INSERTHTML]?.let { insertHtmlState ->
// commandStates.put(CommandName.INSERTLINK, insertHtmlState)
// commandStates.put(CommandName.INSERTIMAGE, insertHtmlState)
// commandStates.put(CommandName.INSERTCHECKBOX, insertHtmlState)
// }
// }
if (url.indexOf(editorStateChangedCallbackScheme) == 0) {
editorStateChanged(
decodedUrl.substring(editorStateChangedCallbackScheme.length));
return true;
}
// String isFormatActivated(CommandState formatCommandState, String format) {
// return (formatCommandState.value == format)
// .toString(); // rich_text_editor.js reports boolean values as string, so we also have to convert it to string
// }
return false;
}
editorStateChanged(String statesString) {
try {
var editorState = EditorState.fromJson(jsonDecode(statesString));
bool currentHtmlChanged = this.htmlField != editorState.html;
this.htmlField = editorState.html;
retrievedEditorState(
editorState.didHtmlChange!, editorState.commandStates!);
if (currentHtmlChanged) {
// fireHtmlChangedListenersAsync(editorState.html);
}
} catch (e) {
throw ("Could not parse command states: $statesString $e");
}
}
retrievedEditorState(
bool didHtmlChange, Map<CommandName, CommandState> commandStates) {
if (this.didHtmlChange != didHtmlChange) {
this.didHtmlChange = didHtmlChange;
// didHtmlChangeListeners.forEach {
// it.didHtmlChange(didHtmlChange);
// }
}
handleRetrievedCommandStates(commandStates);
}
handleRetrievedCommandStates(Map<CommandName, CommandState> commandStates) {
determineDerivedCommandStates(commandStates);
this.commandStates = commandStates;
// commandStatesChangedListeners.forEach {
// it.invoke(this.commandStates)
// }
}
determineDerivedCommandStates(Map<CommandName, CommandState> commandStates) {
// commandStates[CommandName.FORMATBLOCK]?.let {
// formatCommandState
// ->
// commandStates.put(CommandName.H1, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h1")))
// commandStates.put(CommandName.H2, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h2")))
// commandStates.put(CommandName.H3, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h3")))
// commandStates.put(CommandName.H4, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h4")))
// commandStates.put(CommandName.H5, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h5")))
// commandStates.put(CommandName.H6, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "h6")))
// commandStates.put(CommandName.P, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "p")))
// commandStates.put(CommandName.PRE, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "pre")))
// commandStates.put(CommandName.BR, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "")))
// commandStates.put(CommandName.BLOCKQUOTE, CommandState(formatCommandState.executable, isFormatActivated(formatCommandState, "blockquote")))
// }
//
// commandStates[CommandName.INSERTHTML]?.let { insertHtmlState ->
// commandStates.put(CommandName.INSERTLINK, insertHtmlState)
// commandStates.put(CommandName.INSERTIMAGE, insertHtmlState)
// commandStates.put(CommandName.INSERTCHECKBOX, insertHtmlState)
// }
}
String isFormatActivated(CommandState formatCommandState, String format) {
return (formatCommandState.value == format)
.toString(); // rich_text_editor.js reports boolean values as string, so we also have to convert it to string
}
}