headerToOp method
Converts a header HTML element (<h1>
to <h6>
) to Delta operations.
Implementation
@override
List<Operation> headerToOp(dom.Element element) {
final Delta delta = Delta();
Map<String, dynamic> attributes = {};
Map<String, dynamic> blockAttributes = {};
if (element.attributes.containsKey('style') ||
element.attributes.containsKey('align') ||
element.attributes.containsKey('dir')) {
final String style = element.getSafeAttribute('style');
final String styles2 = element.getSafeAttribute('align');
final String styles3 = element.getSafeAttribute('dir');
final styleAttributes = parseStyleAttribute(style);
final alignAttribute = parseStyleAttribute(styles2);
final dirAttribute = parseStyleAttribute(styles3);
styleAttributes.addAll({...alignAttribute, ...dirAttribute});
if (styleAttributes.containsKey('align') ||
styleAttributes.containsKey('direction') ||
styleAttributes.containsKey('indent')) {
blockAttributes['align'] = styleAttributes['align'];
blockAttributes['direction'] = styleAttributes['direction'];
blockAttributes['indent'] = styleAttributes['indent'];
styleAttributes.remove('align');
styleAttributes.remove('direction');
styleAttributes.remove('indent');
}
attributes.addAll(styleAttributes);
}
final headerLevel = element.localName ?? 'h1';
blockAttributes['header'] = int.parse(headerLevel.substring(1));
final nodes = element.nodes;
for (final node in nodes) {
processNode(node, attributes, delta,
addSpanAttrs: true, removeTheseAttributesFromSpan: ['size']);
}
// Ensure a newline is added at the end of the header with the correct attributes
if (blockAttributes.isNotEmpty) {
blockAttributes.removeWhere((key, value) => value == null);
delta.insert('\n', blockAttributes);
}
return delta.toList();
}