insertStyleProperty method
Adds a style-property to the current selection of text. If there is an existing tag around the exact selection, the property gets added to this tag. A -tag will be created otherwise.
Implementation
void insertStyleProperty(StylePropertyOperation op) {
_cache();
op.setSelection(selection);
NodeV2 tree = Parser().parse(text);
int opStart = op.start!;
int opEnd = op.end!;
// pass 1: split only partly affected nodes into new simple nodes
List<SimpleNode> affectedNodes = tree.getNodesInSelection(opStart, opEnd);
_splitOnlyPartiallySelectedNodes(affectedNodes, opStart, opEnd);
// pass 2: apply new tag to all (now only full-selection) nodes
List<SimpleNode> changedNodes = [];
affectedNodes = tree.getNodesInSelection(opStart, opEnd);
for (SimpleNode affectedNode in affectedNodes) {
NodeV2 affectedParent = affectedNode.parent!;
// selection fully contains a node -> insert the attribute in its parent
if (affectedNode.isFullySelected(opStart, opEnd)) {
changedNodes.add(affectedNode);
if (affectedParent.startTagName.isNotEmpty &&
affectedParent.isFullySelected(opStart, opEnd)) {
int offset = _extendNodeWithStyle(affectedParent, op);
// apply the offset to all nodes and the selection end
_applyOffsetToNodes(affectedNode.root, offset, affectedNode);
opEnd += offset;
} else {
int offset = _insertTagNodeBetween(affectedParent, affectedNode,
TagOperation('<span $op>', '</span>', 'span'));
// apply the offset to all nodes and the selection end
_applyOffsetToNodes(affectedNode.root, offset, affectedNode);
opEnd += offset;
}
}
}
if (changedNodes.isNotEmpty) {
int iStart = changedNodes.map((e) => e.textIndexStart).reduce(min);
int iEnd = changedNodes.map((e) => e.textIndexEnd).reduce(max) + 1;
if (editorFocusNode != null) {
editorFocusNode!.requestFocus();
value = value.copyWith(
text: tree.toHtml(),
selection: TextSelection(baseOffset: iStart, extentOffset: iEnd),
);
}
} else {
value = value.copyWith(
text: tree.toHtml(),
);
}
}