mirror of
https://github.com/foss42/apidash.git
synced 2025-12-05 04:18:56 +08:00
Add package json_field_editor
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract class HighlightStrategy {
|
||||
final TextStyle? textStyle;
|
||||
|
||||
HighlightStrategy({required this.textStyle});
|
||||
|
||||
bool match(String word);
|
||||
|
||||
TextSpan textSpan(String word);
|
||||
}
|
||||
|
||||
class KeyHighlightStrategy extends HighlightStrategy {
|
||||
KeyHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'\".*?\"\s*:').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
|
||||
class StringHighlightStrategy extends HighlightStrategy {
|
||||
StringHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'\".*?\"').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
|
||||
class NumberHighlightStrategy extends HighlightStrategy {
|
||||
NumberHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'\s*\b(\d+(\.\d+)?)\b').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
|
||||
class BoolHighlightStrategy extends HighlightStrategy {
|
||||
BoolHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'\b(true|false)\b').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
|
||||
class NullHighlightStrategy extends HighlightStrategy {
|
||||
NullHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'\bnull\b').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
|
||||
class SpecialCharHighlightStrategy extends HighlightStrategy {
|
||||
SpecialCharHighlightStrategy({required super.textStyle});
|
||||
|
||||
@override
|
||||
bool match(String word) => RegExp(r'[{}\[\],:]').hasMatch(word);
|
||||
|
||||
@override
|
||||
TextSpan textSpan(String word) => TextSpan(text: word, style: textStyle);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'package:extended_text_field/extended_text_field.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:json_field_editor/src/json_highlight/highlight_strategy.dart';
|
||||
|
||||
class JsonHighlight extends SpecialTextSpanBuilder {
|
||||
final TextStyle? keyHighlightStyle;
|
||||
final TextStyle? stringHighlightStyle;
|
||||
final TextStyle? numberHighlightStyle;
|
||||
final TextStyle? boolHighlightStyle;
|
||||
final TextStyle? nullHighlightStyle;
|
||||
final TextStyle? specialCharHighlightStyle;
|
||||
final TextStyle? commonTextStyle;
|
||||
final bool isFormating;
|
||||
|
||||
JsonHighlight({
|
||||
this.keyHighlightStyle,
|
||||
this.stringHighlightStyle,
|
||||
this.numberHighlightStyle,
|
||||
this.boolHighlightStyle,
|
||||
this.nullHighlightStyle,
|
||||
this.specialCharHighlightStyle,
|
||||
this.commonTextStyle,
|
||||
required this.isFormating,
|
||||
});
|
||||
|
||||
@override
|
||||
TextSpan build(
|
||||
String data, {
|
||||
TextStyle? textStyle,
|
||||
SpecialTextGestureTapCallback? onTap,
|
||||
}) {
|
||||
List<HighlightStrategy> strategies = [
|
||||
KeyHighlightStrategy(textStyle: keyHighlightStyle),
|
||||
StringHighlightStrategy(textStyle: stringHighlightStyle),
|
||||
NumberHighlightStrategy(textStyle: numberHighlightStyle),
|
||||
BoolHighlightStrategy(textStyle: boolHighlightStyle),
|
||||
NullHighlightStrategy(textStyle: nullHighlightStyle),
|
||||
SpecialCharHighlightStrategy(textStyle: specialCharHighlightStyle),
|
||||
];
|
||||
|
||||
List<TextSpan> spans = [];
|
||||
|
||||
data.splitMapJoin(
|
||||
RegExp(
|
||||
r'\".*?\"\s*:|\".*?\"|\s*\b(\d+(\.\d+)?)\b|\b(true|false|null)\b|[{}\[\],]',
|
||||
),
|
||||
onMatch: (m) {
|
||||
String word = m.group(0)!;
|
||||
if (isFormating) {
|
||||
spans.add(
|
||||
strategies
|
||||
.firstWhere((element) => element.match(word))
|
||||
.textSpan(word),
|
||||
);
|
||||
|
||||
return '';
|
||||
}
|
||||
spans.add(TextSpan(text: word, style: commonTextStyle));
|
||||
return '';
|
||||
},
|
||||
onNonMatch: (n) {
|
||||
spans.add(TextSpan(text: n, style: commonTextStyle));
|
||||
return '';
|
||||
},
|
||||
);
|
||||
|
||||
return TextSpan(children: spans);
|
||||
}
|
||||
|
||||
@override
|
||||
SpecialText? createSpecialText(
|
||||
String flag, {
|
||||
TextStyle? textStyle,
|
||||
SpecialTextGestureTapCallback? onTap,
|
||||
required int index,
|
||||
}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user