Skip to content

2278 doc within doc doc pos with node path #2554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion super_editor/clones/quill/lib/editor/code_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class FeatherCodeComponentBuilder implements ComponentBuilder {
const FeatherCodeComponentBuilder();

@override
SingleColumnLayoutComponentViewModel? createViewModel(Document document, DocumentNode node) {
SingleColumnLayoutComponentViewModel? createViewModel(
Document document,
DocumentNode node,
List<ComponentBuilder> componentBuilders,
) {
if (node is! ParagraphNode) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions super_editor/clones/quill/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ClearSelectedStylesCommand extends EditCommand {
final document = context.find<MutableDocument>(Editor.documentKey);
if (selection.isCollapsed) {
// Remove block style.
final selectedNode = document.getNodeById(selection.extent.nodeId);
final selectedNode = document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is! TextNode) {
// Can't remove text block styles from a non-text node.
return;
Expand Down Expand Up @@ -331,7 +331,7 @@ class ToggleTextBlockFormatCommand extends EditCommand {
}

final document = context.find<MutableDocument>(Editor.documentKey);
final selectedNode = document.getNodeById(selection.extent.nodeId);
final selectedNode = document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is! TextNode) {
// Can't apply a block level text format to a non-text node.
return;
Expand Down Expand Up @@ -492,7 +492,7 @@ class ConvertTextBlockToFormatCommand extends EditCommand {
}

final document = context.find<MutableDocument>(Editor.documentKey);
final selectedNode = document.getNodeById(selection.extent.nodeId);
final selectedNode = document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is! TextNode) {
// Can't apply a block level text format to a non-text node.
return;
Expand Down Expand Up @@ -609,7 +609,7 @@ ExecutionInstruction enterToInsertNewlineInCodeBlock({
if (selection == null || (selection.base.nodeId != selection.extent.nodeId)) {
return ExecutionInstruction.continueExecution;
}
final selectedNode = editContext.document.getNodeById(selection.extent.nodeId)!;
final selectedNode = editContext.document.getNodeById(selection.extent.targetNodeId)!;
if (selectedNode is! ParagraphNode || selectedNode.metadata["blockType"] != codeAttribution) {
return ExecutionInstruction.continueExecution;
}
Expand Down
23 changes: 12 additions & 11 deletions super_editor/clones/quill/lib/editor/toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ class _FormattingToolbarState extends State<FormattingToolbar> {
final selectionEnd = max(baseOffset, extentOffset);
final selectionRange = TextRange(start: selectionStart, end: selectionEnd - 1);

final textNode = widget.editor.document.getNodeById(selection.extent.nodeId) as TextNode;
final textNodePath = selection.extent.documentPath;
final textNode = widget.editor.document.getNodeById(textNodePath.targetNodeId) as TextNode;
final text = textNode.text;

final trimmedRange = _trimTextRangeWhitespace(text, selectionRange);
Expand All @@ -171,11 +172,11 @@ class _FormattingToolbarState extends State<FormattingToolbar> {
AddTextAttributionsRequest(
documentRange: DocumentRange(
start: DocumentPosition(
nodeId: textNode.id,
documentPath: textNodePath,
nodePosition: TextNodePosition(offset: trimmedRange.start),
),
end: DocumentPosition(
nodeId: textNode.id,
documentPath: textNodePath,
nodePosition: TextNodePosition(offset: trimmedRange.end),
),
),
Expand Down Expand Up @@ -249,7 +250,7 @@ class _FormattingToolbarState extends State<FormattingToolbar> {
return;
}

final extentNode = _document.getNodeById(selection.extent.nodeId);
final extentNode = _document.getNodeById(selection.extent.targetNodeId);
if (extentNode is! TextNode) {
return;
}
Expand All @@ -275,7 +276,7 @@ class _FormattingToolbarState extends State<FormattingToolbar> {
return;
}

final extentNode = _document.getNodeById(selection.extent.nodeId);
final extentNode = _document.getNodeById(selection.extent.targetNodeId);
if (extentNode is! TextNode) {
return;
}
Expand All @@ -301,7 +302,7 @@ class _FormattingToolbarState extends State<FormattingToolbar> {
DocumentNode? extentNode;
FeatherTextBlock? selectedBlockFormat;
if (selection != null) {
extentNode = _document.getNodeById(selection.extent.nodeId);
extentNode = _document.getNodeById(selection.extent.targetNodeId);
if (extentNode is TextNode) {
selectedBlockFormat =
selection.base.nodeId == selection.extent.nodeId ? FeatherTextBlock.fromNode(extentNode) : null;
Expand Down Expand Up @@ -702,7 +703,7 @@ class _NamedTextSizeSelectorState extends State<_NamedTextSizeSelector> {
return;
}

final selectedNode = widget.editor.document.getNodeById(selection.extent.nodeId);
final selectedNode = widget.editor.document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is! TextNode) {
return;
}
Expand Down Expand Up @@ -834,7 +835,7 @@ class _HeaderSelectorState extends State<_HeaderSelector> {
return;
}

final selectedNode = widget.editor.document.getNodeById(selection.extent.nodeId);
final selectedNode = widget.editor.document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is! TextNode) {
return;
}
Expand All @@ -853,7 +854,7 @@ class _HeaderSelectorState extends State<_HeaderSelector> {
final selection = composer.selection;
var selectedHeaderLevel = "Normal";
if (selection != null && selection.base.nodeId == selection.extent.nodeId) {
final selectedNode = widget.editor.document.getNodeById(selection.extent.nodeId);
final selectedNode = widget.editor.document.getNodeById(selection.extent.targetNodeId);
if (selectedNode is ParagraphNode) {
selectedHeaderLevel = _headerLevelNames[selectedNode.getMetadataValue("blockType")] ?? "Normal";
}
Expand Down Expand Up @@ -1185,7 +1186,7 @@ class _AlignmentButtonState extends State<_AlignmentButton> {

widget.editor.execute([
ChangeParagraphAlignmentRequest(
nodeId: selection.extent.nodeId,
nodeId: selection.extent.targetNodeId,
alignment: newAlignment,
),
]);
Expand Down Expand Up @@ -1219,7 +1220,7 @@ class _AlignmentButtonState extends State<_AlignmentButton> {
}

final document = widget.editor.document;
final selectedNode = document.getNodeById(selection.extent.nodeId);
final selectedNode = document.getNodeById(selection.extent.targetNodeId);
if (selectedNode == null) {
// Default to "left" when there's no selection. This only effects the
// icon that's displayed on the toolbar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class HeaderWithHintComponentBuilder implements ComponentBuilder {
const HeaderWithHintComponentBuilder();

@override
SingleColumnLayoutComponentViewModel? createViewModel(Document document, DocumentNode node) {
SingleColumnLayoutComponentViewModel? createViewModel(
Document document,
DocumentNode node,
List<ComponentBuilder> componentBuilders,
) {
// This component builder can work with the standard paragraph view model.
// We'll defer to the standard paragraph component builder to create it.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class UnselectableHrComponentBuilder implements ComponentBuilder {
const UnselectableHrComponentBuilder();

@override
SingleColumnLayoutComponentViewModel? createViewModel(Document document, DocumentNode node) {
SingleColumnLayoutComponentViewModel? createViewModel(
Document document,
DocumentNode node,
List<ComponentBuilder> componentBuilders,
) {
// This builder can work with the standard horizontal rule view model, so
// we'll defer to the standard horizontal rule builder.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class AnimatedTaskComponentBuilder implements ComponentBuilder {
const AnimatedTaskComponentBuilder();

@override
SingleColumnLayoutComponentViewModel? createViewModel(Document document, DocumentNode node) {
SingleColumnLayoutComponentViewModel? createViewModel(
Document document,
DocumentNode node,
List<ComponentBuilder> componentBuilders,
) {
// This builder can work with the standard task view model, so
// we'll defer to the standard task builder.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _MobileEditingAndroidDemoState extends State<MobileEditingAndroidDemo> {
return;
}

final selectedNode = _doc.getNodeById(_composer.selection!.extent.nodeId);
final selectedNode = _doc.getNodeById(_composer.selection!.extent.targetNodeId);
if (selectedNode is ListItemNode) {
setState(() {
_imeConfiguration = _imeConfiguration.copyWith(
Expand Down
33 changes: 17 additions & 16 deletions super_editor/example/lib/demos/example_editor/_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ class _EditorToolbarState extends State<EditorToolbar> {
return false;
}

final selectedNode = widget.document.getNodeById(selection.extent.nodeId);
final selectedNode = widget.document.getNodeById(selection.extent.targetNodeId);
return selectedNode is ParagraphNode || selectedNode is ListItemNode;
}

/// Returns the block type of the currently selected text node.
///
/// Throws an exception if the currently selected node is not a text node.
_TextType _getCurrentTextType() {
final selectedNode = widget.document.getNodeById(widget.composer.selection!.extent.nodeId);
final selectedNode = widget.document.getNodeById(widget.composer.selection!.extent.targetNodeId);
if (selectedNode is ParagraphNode) {
final type = selectedNode.getMetadataValue('blockType');

Expand All @@ -149,7 +149,7 @@ class _EditorToolbarState extends State<EditorToolbar> {
///
/// Throws an exception if the currently selected node is not a text node.
TextAlign _getCurrentTextAlignment() {
final selectedNode = widget.document.getNodeById(widget.composer.selection!.extent.nodeId);
final selectedNode = widget.document.getNodeById(widget.composer.selection!.extent.targetNodeId);
if (selectedNode is ParagraphNode) {
final align = selectedNode.getMetadataValue('textAlign');
switch (align) {
Expand Down Expand Up @@ -177,7 +177,7 @@ class _EditorToolbarState extends State<EditorToolbar> {
return false;
}

final selectedNode = widget.document.getNodeById(selection.extent.nodeId);
final selectedNode = widget.document.getNodeById(selection.extent.targetNodeId);
return selectedNode is ParagraphNode;
}

Expand All @@ -197,14 +197,14 @@ class _EditorToolbarState extends State<EditorToolbar> {
if (_isListItem(existingTextType) && _isListItem(newType)) {
widget.editor!.execute([
ChangeListItemTypeRequest(
nodeId: widget.composer.selection!.extent.nodeId,
nodeId: widget.composer.selection!.extent.targetNodeId,
newType: newType == _TextType.orderedListItem ? ListItemType.ordered : ListItemType.unordered,
),
]);
} else if (_isListItem(existingTextType) && !_isListItem(newType)) {
widget.editor!.execute([
ConvertListItemToParagraphRequest(
nodeId: widget.composer.selection!.extent.nodeId,
nodeId: widget.composer.selection!.extent.targetNodeId,
paragraphMetadata: {
'blockType': _getBlockTypeAttribution(newType),
},
Expand All @@ -213,15 +213,15 @@ class _EditorToolbarState extends State<EditorToolbar> {
} else if (!_isListItem(existingTextType) && _isListItem(newType)) {
widget.editor!.execute([
ConvertParagraphToListItemRequest(
nodeId: widget.composer.selection!.extent.nodeId,
nodeId: widget.composer.selection!.extent.targetNodeId,
type: newType == _TextType.orderedListItem ? ListItemType.ordered : ListItemType.unordered,
),
]);
} else {
// Apply a new block type to an existing paragraph node.
widget.editor!.execute([
ChangeParagraphBlockTypeRequest(
nodeId: widget.composer.selection!.extent.nodeId,
nodeId: widget.composer.selection!.extent.targetNodeId,
blockType: _getBlockTypeAttribution(newType),
),
]);
Expand Down Expand Up @@ -325,7 +325,7 @@ class _EditorToolbarState extends State<EditorToolbar> {
final selectionEnd = max(baseOffset, extentOffset);
final selectionRange = SpanRange(selectionStart, selectionEnd - 1);

final textNode = widget.document.getNodeById(selection.extent.nodeId) as TextNode;
final textNode = widget.document.getNodeById(selection.extent.targetNodeId) as TextNode;
final text = textNode.text;

final overlappingLinkAttributions = text.getAttributionSpansInRange(
Expand All @@ -346,7 +346,7 @@ class _EditorToolbarState extends State<EditorToolbar> {
final selectionEnd = max(baseOffset, extentOffset);
final selectionRange = SpanRange(selectionStart, selectionEnd - 1);

final textNode = widget.document.getNodeById(selection.extent.nodeId) as TextNode;
final textNode = widget.document.getNodeById(selection.extent.targetNodeId) as TextNode;
final text = textNode.text;

final overlappingLinkAttributions = text.getAttributionSpansInRange(
Expand Down Expand Up @@ -399,7 +399,8 @@ class _EditorToolbarState extends State<EditorToolbar> {
final selectionEnd = max(baseOffset, extentOffset);
final selectionRange = TextRange(start: selectionStart, end: selectionEnd - 1);

final textNode = widget.document.getNodeById(selection.extent.nodeId) as TextNode;
final selectedNodePath = selection.extent.documentPath;
final textNode = widget.document.getNodeById(selectedNodePath.targetNodeId) as TextNode;
final text = textNode.text;

final trimmedRange = _trimTextRangeWhitespace(text, selectionRange);
Expand All @@ -410,11 +411,11 @@ class _EditorToolbarState extends State<EditorToolbar> {
AddTextAttributionsRequest(
documentRange: DocumentRange(
start: DocumentPosition(
nodeId: textNode.id,
documentPath: selectedNodePath,
nodePosition: TextNodePosition(offset: trimmedRange.start),
),
end: DocumentPosition(
nodeId: textNode.id,
documentPath: selectedNodePath,
nodePosition: TextNodePosition(offset: trimmedRange.end),
),
),
Expand Down Expand Up @@ -459,7 +460,7 @@ class _EditorToolbarState extends State<EditorToolbar> {

widget.editor!.execute([
ChangeParagraphAlignmentRequest(
nodeId: widget.composer.selection!.extent.nodeId,
nodeId: widget.composer.selection!.extent.targetNodeId,
alignment: newAlignment,
),
]);
Expand Down Expand Up @@ -819,11 +820,11 @@ class ImageFormatToolbar extends StatefulWidget {

class _ImageFormatToolbarState extends State<ImageFormatToolbar> {
void _makeImageConfined() {
widget.setWidth(widget.composer.selection!.extent.nodeId, null);
widget.setWidth(widget.composer.selection!.extent.targetNodeId, null);
}

void _makeImageFullBleed() {
widget.setWidth(widget.composer.selection!.extent.nodeId, double.infinity);
widget.setWidth(widget.composer.selection!.extent.targetNodeId, double.infinity);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class _ExampleEditorState extends State<ExampleEditor> {
return;
}

final selectedNode = _doc.getNodeById(selection.extent.nodeId);
final selectedNode = _doc.getNodeById(selection.extent.targetNodeId);

if (selectedNode is ImageNode) {
appLog.fine("Showing image toolbar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class ConvertSelectedTextNodeCommand extends EditCommand {
return;
}

final oldNode = document.getNodeById(composer.selection!.extent.nodeId) as TextNode;
final oldNode = document.getNodeById(composer.selection!.extent.targetNodeId) as TextNode;

late final TextNode newNode;
switch (newType) {
Expand Down
Loading
Loading