Skip to content

Commit

Permalink
Merge pull request #15 from JEuler/reformat
Browse files Browse the repository at this point in the history
Reformat
  • Loading branch information
chandrabezzo committed Dec 21, 2020
2 parents e36ce83 + dad42e6 commit b0c23db
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 86 deletions.
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class _MyHomePageState extends State<MyHomePage> {
),
);
}
}
}
165 changes: 80 additions & 85 deletions lib/flutter_summernote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class FlutterSummernote extends StatefulWidget {
final bool hasAttachment;
final bool showBottomToolbar;

FlutterSummernote({
Key key,
this.value,
this.height,
this.decoration,
this.widthImage:"100%",
this.hint,
this.customToolbar,
this.hasAttachment:false,
this.showBottomToolbar:true
}): super(key: key);
FlutterSummernote(
{Key key,
this.value,
this.height,
this.decoration,
this.widthImage: "100%",
this.hint,
this.customToolbar,
this.hasAttachment: false,
this.showBottomToolbar: true})
: super(key: key);

@override
FlutterSummernoteState createState() => FlutterSummernoteState();
Expand Down Expand Up @@ -83,28 +83,29 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
return Container(
height: widget.height ?? MediaQuery.of(context).size.height,
decoration: widget.decoration ??
BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
border: Border.all(color: Color(0xffececec), width: 1),
),
BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
border: Border.all(color: Color(0xffececec), width: 1),
),
child: Column(
children: <Widget>[
Expanded(
child: WebView(
key: _mapKey,
key: _mapKey,
onWebResourceError: (e) {
print("error ${e.description}");
},
onWebViewCreated: (webViewController) {
_controller = webViewController;
final String contentBase64 = base64Encode(const Utf8Encoder().convert(_page));
final String contentBase64 =
base64Encode(const Utf8Encoder().convert(_page));
_controller.loadUrl('data:text/html;base64,$contentBase64');
},
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
gestureRecognizers: [
Factory(
() => VerticalDragGestureRecognizer()..onUpdate = (_) {}),
() => VerticalDragGestureRecognizer()..onUpdate = (_) {}),
].toSet(),
javascriptChannels: <JavascriptChannel>[
getTextJavascriptChannel(context)
Expand Down Expand Up @@ -135,25 +136,23 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
);
}

List<Widget> _generateBottomToolbar(BuildContext context){
List<Widget> _generateBottomToolbar(BuildContext context) {
var _toolbar = [
Expanded(
child: GestureDetector(
onTap: () async {
String data = await getText();
Clipboard.setData(new ClipboardData(text: data));
},
child: Row(children: <Widget>[
Icon(Icons.content_copy),
Text("Copy")
], mainAxisAlignment: MainAxisAlignment.center),
child: Row(
children: <Widget>[Icon(Icons.content_copy), Text("Copy")],
mainAxisAlignment: MainAxisAlignment.center),
),
),
Expanded(
child: GestureDetector(
onTap: () async {
ClipboardData data =
await Clipboard.getData(Clipboard.kTextPlain);
ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);

String txtIsi = data.text
.replaceAll("'", '\\"')
Expand All @@ -164,29 +163,28 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
.replaceAll("\n\n", "<br/>")
.replaceAll("\r", " ")
.replaceAll('\r\n', " ");
String txt =
"\$('.note-editable').append( '" + txtIsi + "');";
String txt = "\$('.note-editable').append( '" + txtIsi + "');";
_controller.evaluateJavascript(txt);
},
child: Row(children: <Widget>[
Icon(Icons.content_paste),
Text("Paste")
], mainAxisAlignment: MainAxisAlignment.center),
child: Row(
children: <Widget>[Icon(Icons.content_paste), Text("Paste")],
mainAxisAlignment: MainAxisAlignment.center),
),
)
];

if(_hasAttachment){
if (_hasAttachment) {
//add attachment widget
_toolbar.insert(0, Expanded(
child: GestureDetector(
onTap: () => _attach(context),
child: Row(children: <Widget>[
Icon(Icons.attach_file),
Text("Attach")
], mainAxisAlignment: MainAxisAlignment.center),
),
));
_toolbar.insert(
0,
Expanded(
child: GestureDetector(
onTap: () => _attach(context),
child: Row(
children: <Widget>[Icon(Icons.attach_file), Text("Attach")],
mainAxisAlignment: MainAxisAlignment.center),
),
));
}

return _toolbar;
Expand Down Expand Up @@ -265,22 +263,21 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
child: Text(
title,
style: TextStyle(
color: Colors.black54,
fontSize: 16,
fontWeight: FontWeight.w400),
color: Colors.black54,
fontSize: 16,
fontWeight: FontWeight.w400),
),
)
],
),
);
}

String _initPage(String customToolbar){
String _initPage(String customToolbar) {
String toolbar;
if(customToolbar == null){
if (customToolbar == null) {
toolbar = _defaultToolbar;
}
else {
} else {
toolbar = customToolbar;
}

Expand All @@ -293,10 +290,10 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
<title>Summernote</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.js"></script>
</head>
Expand Down Expand Up @@ -326,55 +323,53 @@ class FlutterSummernoteState extends State<FlutterSummernote> {
]
""";

void _attach(BuildContext context){
void _attach(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context){
return Column(children: <Widget>[
ListTile(
leading: Icon(Icons.camera_alt),
title: Text("Camera"),
subtitle: Text("Attach image from camera"),
onTap: () async {
Navigator.pop(context);
final image = await _getImage(true);
if(image != null) _addImage(image);
},
),
ListTile(
leading: Icon(Icons.photo),
title: Text("Gallery"),
subtitle: Text("Attach image from gallery"),
onTap: () async {
Navigator.pop(context);
final image = await _getImage(false);
if(image != null) _addImage(image);
},
),
], mainAxisSize: MainAxisSize.min);
}
);
context: context,
builder: (context) {
return Column(children: <Widget>[
ListTile(
leading: Icon(Icons.camera_alt),
title: Text("Camera"),
subtitle: Text("Attach image from camera"),
onTap: () async {
Navigator.pop(context);
final image = await _getImage(true);
if (image != null) _addImage(image);
},
),
ListTile(
leading: Icon(Icons.photo),
title: Text("Gallery"),
subtitle: Text("Attach image from gallery"),
onTap: () async {
Navigator.pop(context);
final image = await _getImage(false);
if (image != null) _addImage(image);
},
),
], mainAxisSize: MainAxisSize.min);
});
}

Future<File> _getImage(bool fromCamera) async {
final picked = await _imagePicker.getImage(source: (fromCamera) ? ImageSource.camera : ImageSource.gallery);
if(picked != null){
final picked = await _imagePicker.getImage(
source: (fromCamera) ? ImageSource.camera : ImageSource.gallery);
if (picked != null) {
return File(picked.path);
}
else {
} else {
return null;
}
}

void _addImage(File image) async{
void _addImage(File image) async {
String filename = basename(image.path);
List<int> imageBytes = await image.readAsBytes();
String base64Image =
"<img width=\"${widget.widthImage}\" src=\"data:image/png;base64, "
"${base64Encode(imageBytes)}\" data-filename=\"$filename\">";

String txt =
"\$('.note-editable').append( '" + base64Image + "');";
String txt = "\$('.note-editable').append( '" + base64Image + "');";
_controller.evaluateJavascript(txt);
}
}
}

0 comments on commit b0c23db

Please sign in to comment.