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

Update FFI 2.1.2 and SDK 2.17.0 #45

Open
wants to merge 9 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
59 changes: 36 additions & 23 deletions example/lib/highlight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@
* @Description: Code highlight controller
* @Author: ekibun
* @Date: 2020-08-01 17:42:06
* @LastEditors: ekibun
* @LastEditTime: 2020-08-02 12:39:26
* @LastEditors: Matthiee
* @LastEditTime: 2024-06-24 09:34:00
*/
import 'dart:math';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_highlight/themes/a11y-light.dart';
import 'package:highlight/highlight.dart';

Map<String, TextStyle> _theme = a11yLightTheme;

List<TextSpan> _convert(String code) {
var nodes = highlight.parse(code, language: 'javascript').nodes;
var nodes = highlight.parse(code, language: 'javascript').nodes ?? [];
List<TextSpan> spans = [];
var currentSpans = spans;
List<List<TextSpan>> stack = [];

_traverse(Node node) {
if (node.value != null) {
currentSpans.add(node.className == null
? TextSpan(text: node.value)
: TextSpan(text: node.value, style: _theme[node.className]));
} else if (node.children != null) {
final className = node.className;
final value = node.value;
final children = node.children;

if (value != null) {
currentSpans.add(
className == null
? TextSpan(text: value)
: TextSpan(text: value, style: _theme[className]),
);
} else if (children != null) {
List<TextSpan> tmp = [];
currentSpans.add(TextSpan(children: tmp, style: _theme[node.className]));
currentSpans.add(TextSpan(children: tmp, style: _theme[className]));
stack.add(currentSpans);
currentSpans = tmp;

node.children.forEach((n) {
children.forEach((n) {
_traverse(n);
if (n == node.children.last) {
if (n == children.last) {
currentSpans = stack.isEmpty ? spans : stack.removeLast();
}
});
Expand All @@ -47,17 +53,24 @@ List<TextSpan> _convert(String code) {
}

class CodeInputController extends TextEditingController {
CodeInputController({String text}) : super(text: text);
CodeInputController({super.text});

TextSpan oldSpan = TextSpan();
Future<void> spanCall;
Future<void>? spanCall;

@override
TextSpan buildTextSpan(
{@required BuildContext context, TextStyle style, bool withComposing}) {
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing,
}) {
String oldText = oldSpan.toPlainText();
String newText = value.text;
if (oldText == newText) return oldSpan;

if (oldText == newText) {
return oldSpan;
}

(spanCall?.timeout(Duration.zero) ?? Future.value())
.then((_) => spanCall = compute(_convert, value.text).then((lsSpan) {
TextSpan newSpan = TextSpan(style: style, children: lsSpan);
Expand All @@ -66,11 +79,11 @@ class CodeInputController extends TextEditingController {
}))
.catchError((_) => {});

List<TextSpan> beforeSpans = [];
List<InlineSpan> beforeSpans = [];
int splitAt = value.selection.start;
if (splitAt < 0) splitAt = newText.length ~/ 2;
int start = 0;
InlineSpan leftSpan;
InlineSpan? leftSpan;
oldSpan.children?.indexWhere((element) {
String elementText = element.toPlainText();
if (start + elementText.length > splitAt ||
Expand All @@ -82,10 +95,10 @@ class CodeInputController extends TextEditingController {
start += elementText.length;
return false;
});
List<TextSpan> endSpans = [];
List<InlineSpan> endSpans = [];
int end = 0;
InlineSpan rightSpan;
oldSpan.children?.sublist(beforeSpans.length)?.lastIndexWhere((element) {
InlineSpan? rightSpan;
oldSpan.children?.sublist(beforeSpans.length).lastIndexWhere((element) {
String elementText = element.toPlainText();
if (splitAt + end + elementText.length >= newText.length ||
!newText
Expand All @@ -103,7 +116,7 @@ class CodeInputController extends TextEditingController {
...beforeSpans,
TextSpan(
style: leftSpan != null && leftSpan == rightSpan
? leftSpan.style
? leftSpan!.style
: style,
text: newText.substring(start, max(start, newText.length - end))),
...endSpans.reversed
Expand Down
55 changes: 31 additions & 24 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ void main() {
}

class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_qjs',
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(brightness: Brightness.dark, elevation: 0),
backgroundColor: Colors.grey[300],
primaryColorBrightness: Brightness.dark,
),
theme: ThemeData.dark(),
routes: {
'home': (BuildContext context) => TestPage(),
},
Expand All @@ -43,18 +39,26 @@ class TestPage extends StatefulWidget {
}

class _TestPageState extends State<TestPage> {
String resp;
IsolateQjs engine;
String resp = '';
late IsolateQjs engine;

CodeInputController _controller = CodeInputController(
text: 'import("hello").then(({default: greet}) => greet("world"));');
text: 'import("hello").then(({default: greet}) => greet("world"));',
);

@override
void initState() {
super.initState();

_ensureEngine() async {
if (engine != null) return;
engine = IsolateQjs(
engine = _createEngine();
}

IsolateQjs _createEngine() {
return IsolateQjs(
moduleHandler: (String module) async {
return await rootBundle.loadString(
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js",
);
},
);
}
Expand All @@ -77,23 +81,26 @@ class _TestPageState extends State<TestPage> {
TextButton(
child: Text("evaluate"),
onPressed: () async {
await _ensureEngine();
try {
resp = (await engine.evaluate(_controller.text ?? '',
name: "<eval>"))
resp = (await engine.evaluate(
_controller.text,
name: "<eval>",
))
.toString();
setState(() {});
} catch (e) {
resp = e.toString();
setState(() {});
}
setState(() {});
}),
TextButton(
child: Text("reset engine"),
onPressed: () async {
if (engine == null) return;
await engine.close();
engine = null;
}),
child: Text("reset engine"),
onPressed: () async {
final engineToClose = engine;
engine = _createEngine();
await engineToClose.close();
},
),
],
),
),
Expand All @@ -116,7 +123,7 @@ class _TestPageState extends State<TestPage> {
padding: const EdgeInsets.all(12),
color: Colors.green.withOpacity(0.05),
constraints: BoxConstraints(minHeight: 100),
child: Text(resp ?? ''),
child: Text(resp),
),
],
),
Expand Down
Loading