feat: added javascript text commands
This commit is contained in:
32
lib/src/utils/constants.dart
Normal file
32
lib/src/utils/constants.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rich_editor/src/models/button.dart';
|
||||
|
||||
List<Button> buttons = [
|
||||
Button(icon: Icons.format_bold),
|
||||
Button(icon: Icons.format_italic),
|
||||
Button(icon: Icons.format_underline),
|
||||
Button(icon: Icons.format_strikethrough),
|
||||
Button(icon: Icons.superscript),
|
||||
Button(icon: Icons.subscript),
|
||||
Button(icon: Icons.format_clear),
|
||||
Button(icon: Icons.undo),
|
||||
Button(icon: Icons.redo),
|
||||
Button(icon: Icons.format_quote),
|
||||
Button(icon: Icons.text_format),
|
||||
Button(icon: Icons.font_download),
|
||||
Button(icon: Icons.format_size),
|
||||
Button(icon: Icons.format_color_text),
|
||||
Button(icon: Icons.format_color_fill),
|
||||
Button(icon: Icons.format_indent_decrease),
|
||||
Button(icon: Icons.format_indent_increase),
|
||||
Button(icon: Icons.format_align_left_outlined),
|
||||
Button(icon: Icons.format_align_center),
|
||||
Button(icon: Icons.format_align_right),
|
||||
Button(icon: Icons.format_align_justify),
|
||||
Button(icon: Icons.format_list_bulleted),
|
||||
Button(icon: Icons.format_list_numbered),
|
||||
Button(icon: Icons.link),
|
||||
Button(icon: Icons.image),
|
||||
Button(icon: Icons.check_box_outlined),
|
||||
Button(icon: Icons.search),
|
||||
];
|
||||
133
lib/src/utils/javascript_executor_base.dart
Normal file
133
lib/src/utils/javascript_executor_base.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rich_editor/src/extensions/extensions.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class JavaScriptExecutorBase {
|
||||
static executeJavascript(WebViewController controller, String command) async {
|
||||
return await controller.evaluateJavascript('editor.$command');
|
||||
}
|
||||
|
||||
static setHtml(WebViewController controller, String html) async {
|
||||
String? baseUrl;
|
||||
await executeJavascript(
|
||||
controller, "setHtml('" + encodeHtml(html) + "', '$baseUrl');");
|
||||
}
|
||||
|
||||
static getHtml(WebViewController controller) async {
|
||||
String? html = await executeJavascript(controller, 'getEncodedHtml()');
|
||||
String? decodedHtml = Uri.decodeFull(html!);
|
||||
if (decodedHtml.startsWith('"') && decodedHtml.endsWith('"')) {
|
||||
decodedHtml = decodedHtml.substring(1, decodedHtml.length - 1);
|
||||
}
|
||||
return decodedHtml;
|
||||
}
|
||||
|
||||
// Text commands
|
||||
static undo(WebViewController controller) async {
|
||||
await executeJavascript(controller, "undo()");
|
||||
}
|
||||
|
||||
static redo(WebViewController controller) async {
|
||||
await executeJavascript(controller, "redo()");
|
||||
}
|
||||
|
||||
static setBold(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setBold()");
|
||||
}
|
||||
|
||||
static setItalic(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setItalic()");
|
||||
}
|
||||
|
||||
static setUnderline(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setUnderline()");
|
||||
}
|
||||
|
||||
static setSubscript(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setSubscript()");
|
||||
}
|
||||
|
||||
static setSuperscript(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setSuperscript()");
|
||||
}
|
||||
|
||||
static setStrikeThrough(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setStrikeThrough()");
|
||||
}
|
||||
|
||||
static setTextColor(WebViewController controller, Color? color) async {
|
||||
String? hex = color!.toHexColorString();
|
||||
await executeJavascript(controller, "setTextColor('$hex')");
|
||||
}
|
||||
|
||||
static setFontName(WebViewController controller, String fontName) async {
|
||||
await executeJavascript(controller, "setFontName('$fontName')");
|
||||
}
|
||||
|
||||
static setFontSize(WebViewController controller, int fontSize) async {
|
||||
if (fontSize < 1 || fontSize > 7) {
|
||||
throw ("Font size should have a value between 1-7");
|
||||
}
|
||||
await executeJavascript(controller, "setFontSize('$fontSize')");
|
||||
}
|
||||
|
||||
static setHeading(WebViewController controller, int heading) async {
|
||||
await executeJavascript(controller, "setHeading('$heading')");
|
||||
}
|
||||
|
||||
static setFormattingToParagraph(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setFormattingToParagraph()");
|
||||
}
|
||||
|
||||
static setPreformat(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setPreformat()");
|
||||
}
|
||||
|
||||
static setBlockQuote(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setBlockQuote()");
|
||||
}
|
||||
|
||||
static removeFormat(WebViewController controller) async {
|
||||
await executeJavascript(controller, "removeFormat()");
|
||||
}
|
||||
|
||||
static setJustifyLeft(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setJustifyLeft()");
|
||||
}
|
||||
|
||||
static setJustifyCenter(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setJustifyCenter()");
|
||||
}
|
||||
|
||||
static setJustifyRight(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setJustifyRight()");
|
||||
}
|
||||
|
||||
static setJustifyFull(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setJustifyFull()");
|
||||
}
|
||||
|
||||
static setIndent(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setIndent()");
|
||||
}
|
||||
|
||||
static setOutdent(WebViewController controller) async {
|
||||
await executeJavascript(controller, "setOutdent()");
|
||||
}
|
||||
|
||||
static insertBulletList(WebViewController controller) async {
|
||||
await executeJavascript(controller, "insertBulletList()");
|
||||
}
|
||||
|
||||
static insertNumberedList(WebViewController controller) async {
|
||||
await executeJavascript(controller, "insertNumberedList()");
|
||||
}
|
||||
|
||||
static decodeHtml(String html) {
|
||||
return Uri.decodeFull(html);
|
||||
}
|
||||
|
||||
static encodeHtml(String html) {
|
||||
return Uri.encodeFull(html);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user