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

Add insert config #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class _MyHomePageState extends State<MyHomePage> {
//value: "text content initial, if any",
key: keyEditor,
height: 400,
insertConfig: ['link', 'picture', 'video'],
),
Padding(
padding: const EdgeInsets.all(8.0),
Expand Down
5 changes: 3 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ packages:
name: webview_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.7"
version: "0.3.21"
sdks:
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.22.0 <2.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"

133 changes: 93 additions & 40 deletions lib/html_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class HtmlEditor extends StatefulWidget {
final String widthImage;
final bool showBottomToolbar;
final String hint;
final List<String> insertConfig;

HtmlEditor(
{Key key,
Expand All @@ -36,6 +37,7 @@ class HtmlEditor extends StatefulWidget {
this.useBottomSheet = true,
this.widthImage = "100%",
this.showBottomToolbar = true,
this.insertConfig = const ['link', 'picture', 'hr', 'video'],
this.hint})
: super(key: key);

Expand All @@ -47,7 +49,7 @@ class HtmlEditorState extends State<HtmlEditor> {
WebViewController _controller;
String text = "";
final Key _mapKey = UniqueKey();

bool usingImage = false;
int port = 5321;
LocalServer localServer;

Expand All @@ -64,6 +66,53 @@ class HtmlEditorState extends State<HtmlEditor> {
localServer.start(handleRequest);
}


_loadHtmlFromAssets() async {
final filePath = 'packages/html_editor/summernote/summernote.html';
await _controller.loadUrl("http://localhost:$port/$filePath");
await Future.delayed(Duration(seconds: 1));
await _injectSummerNote();
}
_loadHtmlAndroid(String filename) async{
final filename =
'packages/html_editor/summernote/summernote.html';
await _controller.loadUrl(
"file:///android_asset/flutter_assets/" + filename);
//make sure html loaded
await Future.delayed(Duration(seconds: 1));
await _injectSummerNote();
}

_injectSummerNote() async {
var insertConfigString = '';
widget.insertConfig.forEach((element) {
if(element.toLowerCase() == "picture"){
setState(() {
usingImage = true;
});
}
insertConfigString = "'$element',";
});
insertConfigString = "[$insertConfigString]";
print(insertConfigString);
await _controller.evaluateJavascript('''
\$('#summernote-2').summernote({
placeholder: "&nbsp;",
tabsize: 2,
toolbar: [
['style', ['style']],
['font', ['bold', 'underline', 'italic']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['insert', $insertConfigString],
['view', ['codeview']]
],
disableGrammar: false,
spellCheck: false
});
''');
}

void handleRequest(HttpRequest request) {
try {
if (request.method == 'GET' &&
Expand All @@ -85,11 +134,6 @@ class HtmlEditorState extends State<HtmlEditor> {
super.dispose();
}

_loadHtmlFromAssets() async {
final filePath = 'packages/html_editor/summernote/summernote.html';
_controller.loadUrl("http://localhost:$port/$filePath");
}

@override
Widget build(BuildContext context) {
return Container(
Expand All @@ -113,8 +157,7 @@ class HtmlEditorState extends State<HtmlEditor> {
if (Platform.isAndroid) {
final filename =
'packages/html_editor/summernote/summernote.html';
_controller.loadUrl(
"file:///android_asset/flutter_assets/" + filename);
_loadHtmlAndroid(filename);
} else {
_loadHtmlFromAssets();
}
Expand Down Expand Up @@ -153,35 +196,7 @@ class HtmlEditorState extends State<HtmlEditor> {
left: 4.0, right: 4, bottom: 8, top: 2),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
widgetIcon(Icons.image, "Image", onKlik: () {
widget.useBottomSheet
? bottomSheetPickImage(context)
: dialogPickImage(context);
}),
widgetIcon(Icons.content_copy, "Copy", onKlik: () async {
String data = await getText();
Clipboard.setData(new ClipboardData(text: data));
}),
widgetIcon(Icons.content_paste, "Paste",
onKlik: () async {
ClipboardData data =
await Clipboard.getData(Clipboard.kTextPlain);

String txtIsi = data.text
.replaceAll("'", '\\"')
.replaceAll('"', '\\"')
.replaceAll("[", "\\[")
.replaceAll("]", "\\]")
.replaceAll("\n", "<br/>")
.replaceAll("\n\n", "<br/>")
.replaceAll("\r", " ")
.replaceAll('\r\n', " ");
String txt =
"\$('.note-editable').append( '" + txtIsi + "');";
_controller.evaluateJavascript(txt);
}),
],
children: _getBottomToolbar(),
),
)
: Container(
Expand All @@ -192,6 +207,44 @@ class HtmlEditorState extends State<HtmlEditor> {
);
}

List<Widget> _getBottomToolbar(){
List<Widget> _bottomToolbar = [
widgetIcon(Icons.content_copy, "Copy", onKlik: () async {
String data = await getText();
Clipboard.setData(new ClipboardData(text: data));
}),
widgetIcon(Icons.content_paste, "Paste",
onKlik: () async {
ClipboardData data =
await Clipboard.getData(Clipboard.kTextPlain);

String txtIsi = data.text
.replaceAll("'", '\\"')
.replaceAll('"', '\\"')
.replaceAll("[", "\\[")
.replaceAll("]", "\\]")
.replaceAll("\n", "<br/>")
.replaceAll("\n\n", "<br/>")
.replaceAll("\r", " ")
.replaceAll('\r\n', " ");
String txt =
"\$('.note-editable').append( '" + txtIsi + "');";
_controller.evaluateJavascript(txt);
}),
];
if(usingImage){
_bottomToolbar.add(
widgetIcon(Icons.image, "Image", onKlik: () {
widget.useBottomSheet
? bottomSheetPickImage(context)
: dialogPickImage(context);
})
);
}

return _bottomToolbar;
}

JavascriptChannel getTextJavascriptChannel(BuildContext context) {
return JavascriptChannel(
name: 'GetTextSummernote',
Expand Down Expand Up @@ -234,15 +287,15 @@ class HtmlEditorState extends State<HtmlEditor> {

setFullContainer() {
_controller.evaluateJavascript(
'\$("#summernote").summernote("fullscreen.toggle");');
'\$("#summernote-2").summernote("fullscreen.toggle");');
}

setFocus() {
_controller.evaluateJavascript("\$('#summernote').summernote('focus');");
_controller.evaluateJavascript("\$('#summernote-2').summernote('focus');");
}

setEmpty() {
_controller.evaluateJavascript("\$('#summernote').summernote('reset');");
_controller.evaluateJavascript("\$('#summernote-2').summernote('reset');");
}

setHint(String text) {
Expand Down
17 changes: 2 additions & 15 deletions lib/summernote/summernote.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,9 @@
<script src="summernote-lite.min.js"></script>
</head>
<body>
<div id="summernote" style="width:100%;height:100%"></div>
<div id="summernote-2" style="width:100%;height:100%"></div>
<script>
$('#summernote').summernote({
placeholder: "&nbsp;",
tabsize: 2,
toolbar: [
['style', ['style']],
['font', ['bold', 'underline', 'italic']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['insert', ['link', 'picture', 'hr', 'video']],
['view', ['codeview']]
],
disableGrammar: false,
spellCheck: false
});

</script>
<style>
body {
Expand Down
5 changes: 3 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ packages:
name: webview_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.7"
version: "0.3.21"
sdks:
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.22.0 <2.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"