add Ignore Rule

pull/31/head
ritheshSalyan 4 years ago
parent e1d6e2820e
commit 717ceeb85d
  1. 38
      lib/models/rules/format.dart
  2. 3
      lib/models/rules/rule.dart

@ -132,3 +132,41 @@ class ResolveInlineFormatRule extends FormatRule {
return delta;
}
}
class ResolveInlineIgnoreFormatRule extends FormatRule {
const ResolveInlineIgnoreFormatRule();
@override
Delta applyRule(Delta document, int index,
{int len, Object data, Attribute attribute}) {
if (attribute.scope != AttributeScope.IGNORE) {
return null;
}
Delta delta = Delta()..retain(index, attribute.toJson());
DeltaIterator itr = DeltaIterator(document);
itr.skip(index);
Operation op;
for (int cur = 0; cur < len && itr.hasNext; cur += op.length) {
op = itr.next(len - cur);
String text = op.data is String ? op.data as String : '';
int lineBreak = text.indexOf('\n');
if (lineBreak < 0) {
delta.retain(op.length, attribute.toJson());
continue;
}
int pos = 0;
while (lineBreak >= 0) {
delta..retain(lineBreak - pos, attribute.toJson())..retain(1);
pos = lineBreak + 1;
lineBreak = text.indexOf('\n', pos);
}
if (pos < op.length) {
delta.retain(op.length - pos, attribute.toJson());
}
}
return delta;
}
}

@ -34,6 +34,7 @@ class Rules {
FormatLinkAtCaretPositionRule(),
ResolveLineFormatRule(),
ResolveInlineFormatRule(),
ResolveInlineIgnoreFormatRule(),
InsertEmbedsRule(),
ForceNewlineForInsertsAroundEmbedRule(),
AutoExitBlockRule(),
@ -70,6 +71,6 @@ class Rules {
throw e;
}
}
throw ('Apply rules failed');
// throw ('Apply rules failed');
}
}

Loading…
Cancel
Save