Rich text editor for Flutter
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.

78 lines
2.0 KiB

import '../documents/attribute.dart';
import '../documents/document.dart';
import '../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}) {
validateArgs(len, data, attribute);
return applyRule(document, index,
len: len, data: data, attribute: attribute);
}
4 years ago
void validateArgs(int? len, Object? data, Attribute? attribute);
Delta? applyRule(Delta document, int index,
{int? len, Object? data, Attribute? attribute});
RuleType get type;
}
class Rules {
Rules(this._rules);
merge conflict (#257) * Update issue templates * Updating checkbox to handle tap (#186) * updating checkbox to handle tap * updating checkbox to handle long press and using UniqueKey() to avoid weird side effects * removed useless doc Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> * Simple viewer (#187) * 2021-04-25 * 2021-04-26 * Fix simple viewer compilation error * Upgrade version - checkbox supports tapping * 171: support for non-scrollable text editor (#188) Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> * custom rules & optionally auto add newline for image embeds (#205) * fixbug: #219 FocusOnKeyCallback need return a keyeventresult * Adding missing overrides to make package work with Flutter 2.2.0 (#226) * Improve SOC of raw editor (#227) Improve separation of concerns for RawEditor by moving the code for the text input client to a separate class, furthermore add more comments. * Upgrade version * Improve SOC of raw editor (#228) Improve separation of concerns for `RawEditor` by moving the code for the keyboard to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * Improve further SOC of raw editor This improves separation of concerns for the RawEditor by moving the code for the text selection delegate to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * toolbar add multi-line display #229 Add a parameter to Toolbar, it supports multi-line display * bugfix: can't drag select text bugfix: can't drag select text Co-authored-by: Xin Yao <singerdmx@gmail.com> Co-authored-by: kevinDespoulains <46108869+kevinDespoulains@users.noreply.github.com> Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> Co-authored-by: em6m6e <50019687+em6m6e@users.noreply.github.com> Co-authored-by: Gyuri Majercsik <majercsik.gyuri@gmail.com> Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> Co-authored-by: hyouuu <hyouuu@gmail.com> Co-authored-by: Till Friebe <friebetill@gmail.com>
4 years ago
List<Rule> _customRules = [];
final List<Rule> _rules;
static final Rules _instance = Rules([
const FormatLinkAtCaretPositionRule(),
const ResolveLineFormatRule(),
const ResolveInlineFormatRule(),
const InsertEmbedsRule(),
const ForceNewlineForInsertsAroundEmbedRule(),
const AutoExitBlockRule(),
const PreserveBlockStyleOnInsertRule(),
const PreserveLineStyleOnSplitRule(),
const ResetLineFormatOnNewLineRule(),
const AutoFormatLinksRule(),
const PreserveInlineStylesRule(),
const CatchAllInsertRule(),
const EnsureEmbedLineRule(),
const PreserveLineStyleOnMergeRule(),
const CatchAllDeleteRule(),
]);
static Rules getInstance() => _instance;
merge conflict (#257) * Update issue templates * Updating checkbox to handle tap (#186) * updating checkbox to handle tap * updating checkbox to handle long press and using UniqueKey() to avoid weird side effects * removed useless doc Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> * Simple viewer (#187) * 2021-04-25 * 2021-04-26 * Fix simple viewer compilation error * Upgrade version - checkbox supports tapping * 171: support for non-scrollable text editor (#188) Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> * custom rules & optionally auto add newline for image embeds (#205) * fixbug: #219 FocusOnKeyCallback need return a keyeventresult * Adding missing overrides to make package work with Flutter 2.2.0 (#226) * Improve SOC of raw editor (#227) Improve separation of concerns for RawEditor by moving the code for the text input client to a separate class, furthermore add more comments. * Upgrade version * Improve SOC of raw editor (#228) Improve separation of concerns for `RawEditor` by moving the code for the keyboard to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * Improve further SOC of raw editor This improves separation of concerns for the RawEditor by moving the code for the text selection delegate to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * toolbar add multi-line display #229 Add a parameter to Toolbar, it supports multi-line display * bugfix: can't drag select text bugfix: can't drag select text Co-authored-by: Xin Yao <singerdmx@gmail.com> Co-authored-by: kevinDespoulains <46108869+kevinDespoulains@users.noreply.github.com> Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> Co-authored-by: em6m6e <50019687+em6m6e@users.noreply.github.com> Co-authored-by: Gyuri Majercsik <majercsik.gyuri@gmail.com> Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> Co-authored-by: hyouuu <hyouuu@gmail.com> Co-authored-by: Till Friebe <friebetill@gmail.com>
4 years ago
void setCustomRules(List<Rule> customRules) {
_customRules = customRules;
}
Delta apply(RuleType ruleType, Document document, int index,
{int? len, Object? data, Attribute? attribute}) {
5 years ago
final delta = document.toDelta();
merge conflict (#257) * Update issue templates * Updating checkbox to handle tap (#186) * updating checkbox to handle tap * updating checkbox to handle long press and using UniqueKey() to avoid weird side effects * removed useless doc Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> * Simple viewer (#187) * 2021-04-25 * 2021-04-26 * Fix simple viewer compilation error * Upgrade version - checkbox supports tapping * 171: support for non-scrollable text editor (#188) Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> * custom rules & optionally auto add newline for image embeds (#205) * fixbug: #219 FocusOnKeyCallback need return a keyeventresult * Adding missing overrides to make package work with Flutter 2.2.0 (#226) * Improve SOC of raw editor (#227) Improve separation of concerns for RawEditor by moving the code for the text input client to a separate class, furthermore add more comments. * Upgrade version * Improve SOC of raw editor (#228) Improve separation of concerns for `RawEditor` by moving the code for the keyboard to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * Improve further SOC of raw editor This improves separation of concerns for the RawEditor by moving the code for the text selection delegate to a separate class, furthermore add more comments. The PR does not change the functionality of the code. * toolbar add multi-line display #229 Add a parameter to Toolbar, it supports multi-line display * bugfix: can't drag select text bugfix: can't drag select text Co-authored-by: Xin Yao <singerdmx@gmail.com> Co-authored-by: kevinDespoulains <46108869+kevinDespoulains@users.noreply.github.com> Co-authored-by: Kevin Despoulains <kevin.despoulains@scriptandgo.com> Co-authored-by: em6m6e <50019687+em6m6e@users.noreply.github.com> Co-authored-by: Gyuri Majercsik <majercsik.gyuri@gmail.com> Co-authored-by: Gyuri Majercsik <gyuri@fluttech.com> Co-authored-by: hyouuu <hyouuu@gmail.com> Co-authored-by: Till Friebe <friebetill@gmail.com>
4 years ago
for (final rule in _customRules + _rules) {
if (rule.type != ruleType) {
continue;
}
5 years ago
try {
final result = rule.apply(delta, index,
len: len, data: data, attribute: attribute);
if (result != null) {
return result..trim();
}
} catch (e) {
4 years ago
rethrow;
}
}
throw 'Apply rules failed';
}
}