Skip to content

Commit

Permalink
Add checkBoxReadOnly property
Browse files Browse the repository at this point in the history
  • Loading branch information
gklamm committed Apr 26, 2024
1 parent e7774c7 commit 3e488eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
32 changes: 23 additions & 9 deletions lib/doc-tree/services/doc-tree.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class DocTreeService {
List<Widget> buildDocumentTree() {
final docWidgets = <Widget>[];
var indentLevelCounts = <int, int>{};
final nodes = _placeholderService.getDocOrPlaceholderCtrl().rootNode.children;
final nodes =
_placeholderService.getDocOrPlaceholderCtrl().rootNode.children;
final renderers = <EditableTextLineWidgetRenderer>[];

for (final node in nodes) {
Expand Down Expand Up @@ -287,30 +288,42 @@ class DocTreeService {
final int? level = attrs[AttributesM.header.key]!.value;
switch (level) {
case 1:
return isLastLine ? defaultStyles!.h1!.lastLineSpacing : defaultStyles!.h1!.verticalSpacing;
return isLastLine
? defaultStyles!.h1!.lastLineSpacing
: defaultStyles!.h1!.verticalSpacing;
case 2:
return isLastLine ? defaultStyles!.h2!.lastLineSpacing : defaultStyles!.h2!.verticalSpacing;
return isLastLine
? defaultStyles!.h2!.lastLineSpacing
: defaultStyles!.h2!.verticalSpacing;
case 3:
return isLastLine ? defaultStyles!.h3!.lastLineSpacing : defaultStyles!.h3!.verticalSpacing;
return isLastLine
? defaultStyles!.h3!.lastLineSpacing
: defaultStyles!.h3!.verticalSpacing;
default:
throw 'Invalid level $level';
}
}

return isLastLine ? defaultStyles!.paragraph!.lastLineSpacing : defaultStyles!.paragraph!.verticalSpacing;
return isLastLine
? defaultStyles!.paragraph!.lastLineSpacing
: defaultStyles!.paragraph!.verticalSpacing;
}

// Updates the checkbox positioned at [offset] in document by changing its attribute according to [value].
void _handleCheckboxTap(int offset, bool value) {
if (!state.config.readOnly) {
if (!(state.config.checkBoxReadOnly ?? state.config.readOnly)) {
state.scrollAnimation.disabled = true;
final attribute = value ? AttributesAliasesM.checked : AttributesAliasesM.unchecked;
final attribute =
value ? AttributesAliasesM.checked : AttributesAliasesM.unchecked;

_stylesService.formatTextRange(offset, 0, attribute);

// Checkbox tapping causes text selection to go to offset 0.
// Stop toggling those two buttons.
state.toolbar.buttonToggler = {AttributesM.list.key: attribute, AttributesM.header.key: AttributesM.header};
state.toolbar.buttonToggler = {
AttributesM.list.key: attribute,
AttributesM.header.key: AttributesM.header
};

// Go back from offset 0 to current selection.
SchedulerBinding.instance.addPostFrameCallback((_) {
Expand All @@ -324,7 +337,8 @@ class DocTreeService {

Future<LinkMenuAction> _linkActionPicker(NodeM linkNode) async {
final link = linkNode.style.attributes[AttributesM.link.key]!.value!;
final linkDelegate = state.config.linkActionPickerDelegate ?? defaultLinkActionPickerDelegate;
final linkDelegate = state.config.linkActionPickerDelegate ??
defaultLinkActionPickerDelegate;

return linkDelegate(
state.refs.widget.context,
Expand Down
4 changes: 2 additions & 2 deletions lib/doc-tree/widgets/editable-text-block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class EditableTextBlock extends StatelessWidget {
return CheckboxPoint(
size: 14,
value: true,
enabled: !_state.config.readOnly,
enabled: !(_state.config.checkBoxReadOnly ?? _state.config.readOnly),
onChanged: (checked) => onCheckboxTap(lineOffset, checked),
uiBuilder: styles.lists?.checkboxUIBuilder,
);
Expand All @@ -228,7 +228,7 @@ class EditableTextBlock extends StatelessWidget {
return CheckboxPoint(
size: 14,
value: false,
enabled: !_state.config.readOnly,
enabled: !(_state.config.checkBoxReadOnly ?? _state.config.readOnly),
onChanged: (checked) => onCheckboxTap(lineOffset, checked),
uiBuilder: styles.lists?.checkboxUIBuilder,
);
Expand Down
31 changes: 23 additions & 8 deletions lib/editor/models/editor-cfg.model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class EditorConfigM {
// The text remains selectable.
final bool readOnly;

// Override [readOnly] for checkbox.
// When this is set to `false`, the checkbox can be checked or unchecked while [readOnly] is set to `true`.
// When this is set to `true`, the checkbox cannot be checked or unchecked while [readOnly] is set to `false`.
// When this is set to `null`, the [readOnly] value is used.
final bool? checkBoxReadOnly;

// Content to be displayed when there is no content in the Delta document
final String? placeholder;

Expand Down Expand Up @@ -198,6 +204,7 @@ class EditorConfigM {
this.padding = EdgeInsets.zero,
this.autoFocus = false,
this.readOnly = false,
this.checkBoxReadOnly,
this.expands = false,
this.paintCursorAboveText,
this.placeholder,
Expand Down Expand Up @@ -254,6 +261,7 @@ class EditorConfigM {
bool? autoFocus,
bool? paintCursorAboveText,
bool? readOnly,
bool? checkBoxReadOnly,
String? placeholder,
bool? enableInteractiveSelection,
double? minHeight,
Expand Down Expand Up @@ -299,8 +307,10 @@ class EditorConfigM {
autoFocus: autoFocus ?? this.autoFocus,
paintCursorAboveText: paintCursorAboveText ?? this.paintCursorAboveText,
readOnly: readOnly ?? this.readOnly,
checkBoxReadOnly: checkBoxReadOnly ?? this.checkBoxReadOnly,
placeholder: placeholder ?? this.placeholder,
enableInteractiveSelection: enableInteractiveSelection ?? this.enableInteractiveSelection,
enableInteractiveSelection:
enableInteractiveSelection ?? this.enableInteractiveSelection,
minHeight: minHeight ?? this.minHeight,
maxHeight: maxHeight ?? this.maxHeight,
maxContentWidth: maxContentWidth ?? this.maxContentWidth,
Expand All @@ -310,26 +320,32 @@ class EditorConfigM {
keyboardAppearance: keyboardAppearance ?? this.keyboardAppearance,
scrollPhysics: scrollPhysics ?? this.scrollPhysics,
keepStyleOnNewLine: keepStyleOnNewLine ?? this.keepStyleOnNewLine,
overrideEmbedBuilders: overrideEmbedBuilders ?? this.overrideEmbedBuilders,
overrideEmbedBuilders:
overrideEmbedBuilders ?? this.overrideEmbedBuilders,
customEmbedBuilders: customEmbedBuilders ?? this.customEmbedBuilders,
customStyleBuilder: customStyleBuilder ?? this.customStyleBuilder,
locale: locale ?? this.locale,
linkActionPickerDelegate: linkActionPickerDelegate ?? this.linkActionPickerDelegate,
linkActionPickerDelegate:
linkActionPickerDelegate ?? this.linkActionPickerDelegate,
linkMenuDisabled: linkMenuDisabled ?? this.linkMenuDisabled,
floatingCursorDisabled: floatingCursorDisabled ?? this.floatingCursorDisabled,
floatingCursorDisabled:
floatingCursorDisabled ?? this.floatingCursorDisabled,
forcePressEnabled: forcePressEnabled ?? this.forcePressEnabled,
selection: selection ?? this.selection,
textSelectionControls: textSelectionControls ?? this.textSelectionControls,
textSelectionControls:
textSelectionControls ?? this.textSelectionControls,
highlights: highlights ?? this.highlights,
markerTypes: markerTypes ?? this.markerTypes,
markersVisibility: markersVisibility ?? this.markersVisibility,
onTapDown: onTapDown ?? this.onTapDown,
onTapUp: onTapUp ?? this.onTapUp,
onSingleLongTapStart: onSingleLongTapStart ?? this.onSingleLongTapStart,
onSingleLongTapMoveUpdate: onSingleLongTapMoveUpdate ?? this.onSingleLongTapMoveUpdate,
onSingleLongTapMoveUpdate:
onSingleLongTapMoveUpdate ?? this.onSingleLongTapMoveUpdate,
onSingleLongTapEnd: onSingleLongTapEnd ?? this.onSingleLongTapEnd,
onReplaceText: onReplaceText ?? this.onReplaceText,
onReplaceTextCompleted: onReplaceTextCompleted ?? this.onReplaceTextCompleted,
onReplaceTextCompleted:
onReplaceTextCompleted ?? this.onReplaceTextCompleted,
onDelete: onDelete ?? this.onDelete,
onSelectionChanged: onSelectionChanged ?? this.onSelectionChanged,
onSelectionCompleted: onSelectionCompleted ?? this.onSelectionCompleted,
Expand All @@ -339,4 +355,3 @@ class EditorConfigM {
);
}
}

0 comments on commit 3e488eb

Please sign in to comment.