dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.8 KiB
68 lines
1.8 KiB
4 years ago
|
import 'package:flutter_quill/models/documents/attribute.dart';
|
||
|
import 'package:flutter_quill/models/documents/document.dart';
|
||
|
import 'package:quill_delta/quill_delta.dart';
|
||
|
|
||
|
import 'delete.dart';
|
||
|
import 'format.dart';
|
||
|
import 'insert.dart';
|
||
|
|
||
|
enum RuleType { INSERT, DELETE, FORMAT }
|
||
|
|
||
|
abstract class Rule {
|
||
|
const Rule();
|
||
|
|
||
|
Delta apply(Delta document, int index,
|
||
|
{int len, Object data, Attribute attribute}) {
|
||
|
assert(document != null);
|
||
|
assert(index != null);
|
||
|
validateArgs(len, data, attribute);
|
||
|
return applyRule(document, index,
|
||
|
len: len, data: data, attribute: attribute);
|
||
|
}
|
||
|
|
||
|
validateArgs(int len, Object data, Attribute attribute);
|
||
|
|
||
|
Delta applyRule(Delta document, int index,
|
||
|
{int len, Object data, Attribute attribute});
|
||
|
|
||
|
RuleType get type;
|
||
|
}
|
||
|
|
||
|
class Rules {
|
||
|
final List<Rule> _rules;
|
||
|
static final Rules _instance = Rules([
|
||
|
FormatLinkAtCaretPositionRule(),
|
||
|
ResolveLineFormatRule(),
|
||
|
ResolveInlineFormatRule(),
|
||
|
InsertEmbedsRule(),
|
||
|
ForceNewlineForInsertsAroundEmbedRule(),
|
||
|
AutoExitBlockRule(),
|
||
|
PreserveBlockStyleOnInsertRule(),
|
||
|
PreserveLineStyleOnSplitRule(),
|
||
|
ResetLineFormatOnNewLineRule(),
|
||
|
AutoFormatLinksRule(),
|
||
|
PreserveInlineStylesRule(),
|
||
|
CatchAllInsertRule(),
|
||
|
EnsureEmbedLineRule(),
|
||
|
PreserveLineStyleOnMergeRule(),
|
||
|
CatchAllDeleteRule(),
|
||
|
]);
|
||
|
|
||
|
Rules(this._rules);
|
||
|
|
||
|
static Rules getInstance() => _instance;
|
||
|
|
||
|
Delta apply(RuleType ruleType, Document document, int index,
|
||
|
{int len, Object data, Attribute attribute}) {
|
||
|
Delta delta = document.toDelta();
|
||
|
for (var rule in _rules) {
|
||
|
delta =
|
||
|
rule.apply(delta, index, len: len, data: data, attribute: attribute);
|
||
|
if (delta != null) {
|
||
|
return delta..trim();
|
||
|
}
|
||
|
}
|
||
|
throw('Apply rules failed');
|
||
|
}
|
||
|
}
|