Skip to content
494 changes: 372 additions & 122 deletions lib/plugins/view/plugin_correction_controller.dart

Large diffs are not rendered by default.

306 changes: 280 additions & 26 deletions lib/plugins/view/plugin_correction_paragraph.dart

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions lib/plugins/view/plugin_correction_projection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'dart:typed_data';

import 'package:otzaria/utils/text/text_manipulation.dart' as utils;

/// הטקסט המלא של פסקה והטקסט הגלוי ממנו, כפי שהקורא מציג אותו (ניקוד וטעמים
/// מוסתרים לפי ההגדרות), עם מיפוי היסטים ביניהם.
class CorrectionProjection {
final String full;
final bool nikud;
final bool teamim;

/// הטקסט הגלוי: בדיוק מה ש-[utils.removeMarks] מחזיר, כמו בקורא.
final String visible;

/// היסט בטקסט המלא של כל תו גלוי, ובסוף אורך הטקסט המלא. null כשהכול גלוי.
final Int32List? _toFull;

CorrectionProjection._(
this.full,
this.nikud,
this.teamim,
this.visible,
this._toFull,
);

factory CorrectionProjection(
String full, {
required bool nikud,
required bool teamim,
}) {
final visible = utils.removeMarks(full, nikud: nikud, teamim: teamim);
final toFull = visible == full
? null
: _align(full, visible, nikud, teamim);
// יישור שנכשל משאיר את הטקסט המלא גלוי: עדיף מסך עמוס מאשר עריכה שגויה.
return toFull == null && visible != full
? CorrectionProjection._(full, nikud, teamim, full, null)
: CorrectionProjection._(full, nikud, teamim, visible, toFull);
}

static final Map<int, String> _images = {};

/// מה שהקורא מציג במקום יחידת קוד אחת (ריק כשהיא מוסתרת).
static String _image(int unit, bool nikud, bool teamim) =>
_images.putIfAbsent(
unit | (nikud ? 1 << 16 : 0) | (teamim ? 1 << 17 : 0),
() => utils.removeMarks(
String.fromCharCode(unit),
nikud: nikud,
teamim: teamim,
),
);

/// מתאים כל תו גלוי ליחידה שבטקסט המלא, מהסוף להתחלה: כשהקורא מוחק רווח
/// שלפני פסק, הרווח שנשאר גלוי הוא האחרון.
static Int32List? _align(
String full,
String visible,
bool nikud,
bool teamim,
) {
final toFull = Int32List(visible.length + 1);
toFull[visible.length] = full.length;
var j = visible.length - 1;
for (var k = full.length - 1; k >= 0 && j >= 0; k--) {
final image = _image(full.codeUnitAt(k), nikud, teamim);
if (image.length == 1 && image.codeUnitAt(0) == visible.codeUnitAt(j)) {
toFull[j--] = k;
}
}
return j < 0 ? toFull : null;
}

/// ההיסט בטקסט המלא של התו הגלוי שב-[visibleOffset]. סימנים מוסתרים שאחרי
/// תו שייכים אליו, ולכן נכנסים לטווח שמסתיים אחריו.
int fullOffset(int visibleOffset) =>
_toFull == null ? visibleOffset : _toFull[visibleOffset];

/// ההיסט הגלוי הראשון שאינו לפני [fullOffset].
int visibleOffset(int fullOffset) {
final toFull = _toFull;
if (toFull == null) return fullOffset.clamp(0, visible.length);
var low = 0;
var high = visible.length;
while (low < high) {
final middle = (low + high) >> 1;
if (toFull[middle] < fullOffset) {
low = middle + 1;
} else {
high = middle;
}
}
return low;
}

CorrectionProjection withHiding({
required bool nikud,
required bool teamim,
}) => nikud == this.nikud && teamim == this.teamim
? this
: CorrectionProjection(full, nikud: nikud, teamim: teamim);
}
46 changes: 24 additions & 22 deletions lib/widgets/text/rtl_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -869,24 +869,36 @@ class _RtlTextFieldState extends State<RtlTextField> {
return;
}

void replaceSelection(String replacement) {
if (!mounted ||
controller != _effectiveController ||
controller.text != currentText) {
return;
}
if (controller.selection != currentSelection) {
controller.selection = currentSelection;
}
controller.value = TextEditingValue(
text: currentText.replaceRange(
currentSelection.start,
currentSelection.end,
replacement,
),
selection: TextSelection.collapsed(
offset: currentSelection.start + replacement.length,
),
);
widget.onChanged?.call(controller.text);
}

switch (value) {
case 'cut':
final selectedText = currentText.substring(
currentSelection.start,
currentSelection.end,
);
await Clipboard.setData(ClipboardData(text: selectedText));
if (!mounted || controller != _effectiveController) return;
final textAfterCut =
currentText.substring(0, currentSelection.start) +
currentText.substring(currentSelection.end);
controller.text = textAfterCut;
controller.selection = TextSelection.collapsed(
offset: currentSelection.start,
);
// עדכון ידני: הקצאה ישירה ל-controller.text לא מפעילה את onChanged
// של TextField (זה מגיע רק מנתיב הקלט הפנימי של EditableText).
widget.onChanged?.call(textAfterCut);
replaceSelection('');
break;
case 'copy':
final selectedText = currentText.substring(
Expand All @@ -904,17 +916,7 @@ class _RtlTextFieldState extends State<RtlTextField> {
final pasted = _hardBreakWorkaroundActive
? collapseHardBreaks(data!.text!)
: data!.text!;
final newText =
currentText.substring(0, currentSelection.start) +
pasted +
currentText.substring(currentSelection.end);
controller.text = newText;
controller.selection = TextSelection.collapsed(
offset: currentSelection.start + pasted.length,
);
// עדכון ידני: הקצאה ישירה ל-controller.text לא מפעילה את onChanged
// של TextField (זה מגיע רק מנתיב הקלט הפנימי של EditableText).
widget.onChanged?.call(newText);
replaceSelection(pasted);
}
break;
case 'selectAll':
Expand Down
122 changes: 112 additions & 10 deletions test/plugins/view/plugin_correction_controller_test.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:otzaria/plugins/view/plugin_correction_controller.dart';
import 'package:otzaria/utils/text/text_manipulation.dart' as utils;

void main() {
for (final source in ['<br>אב', 'אב<br>', '<br>אב<br>']) {
test('המרה סימטרית גם כשהתיקון הסיר את שולי $source', () {
final controller = PluginCorrectionController(source);
addTearDown(controller.dispose);
for (final canonical in [controller.source.plain, 'אם', '', ' אמת ']) {
expect(
controller.forService(controller.forEditor(canonical)),
canonical,
);
controller.restoreCanonical(canonical);
expect(controller.canonical, canonical);
}
});
}
Expand All @@ -21,10 +20,10 @@ void main() {
addTearDown(controller.dispose);
controller.setSource('<p>אב</p>');
expect(controller.text, 'אב');
expect(controller.forService(controller.text), ' אב ');
expect(controller.canonical, ' אב ');
controller.setSource('<i>גד</i>');
expect(controller.text, 'גד');
expect(controller.forService(controller.text), 'גד');
expect(controller.canonical, 'גד');
});

test('הד חוזר של אותו מקור משמר בחירה ו-IME', () {
Expand Down Expand Up @@ -99,7 +98,7 @@ void main() {
final controller = PluginCorrectionController(source);
addTearDown(controller.dispose);
controller.text = 'אם גד';
expect(controller.forService(controller.text), ' אם גד ');
expect(controller.canonical, ' אם גד ');
expect(controller.formats.length, controller.text.length);
});

Expand Down Expand Up @@ -341,15 +340,15 @@ void main() {
test('שחזור טיוטה עם מופע חוזר אינו מנחש שהמופע המודגש שרד', () {
final controller = PluginCorrectionController('<b>אב</b> אב <i>גד</i>');
addTearDown(controller.dispose);
controller.restoreDraftValue(const TextEditingValue(text: 'אב גד'));
controller.restoreCanonical('אב גד');
expect(controller.formats.take(2).every((f) => !f.bold), isTrue);
expect(controller.formats.skip(3).every((f) => f.italic), isTrue);
});

test('שחזור טיוטה משמר מופעים חוזרים כשנערכה רק מילה אחרת', () {
final controller = PluginCorrectionController('<b>אב</b> אב גד');
addTearDown(controller.dispose);
controller.restoreDraftValue(const TextEditingValue(text: 'אב אב גה'));
controller.restoreCanonical('אב אב גה');
expect(controller.formats.map((f) => f.bold), [
true,
true,
Expand All @@ -365,7 +364,7 @@ void main() {
test('שחזור מילה מעורבת אינו מעצב תו פנימי שלא השתנה', () {
final controller = PluginCorrectionController('<b>א</b>בג');
addTearDown(controller.dispose);
controller.restoreDraftValue(const TextEditingValue(text: 'הבד'));
controller.restoreCanonical('הבד');
expect(controller.formats.every((f) => !f.bold), isTrue);
});

Expand All @@ -384,4 +383,107 @@ void main() {
expect(controller.formats[3].italic, isFalse);
});
});

group('הסתרת ניקוד וטעמים', () {
const patah = '\u05B7';
const holam = '\u05B9';
const samples = [
'בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֣ת־הַשָּׁמַ֔יִם',
'אב ׀ גד',
'אב־׀גד ׀',
];

for (final sample in samples) {
for (final nikud in [false, true]) {
for (final teamim in [false, true]) {
test('מחיקת כל תו בנפרד: "$sample" ניקוד=$nikud טעמים=$teamim', () {
final shown = utils.removeMarks(
sample,
nikud: nikud,
teamim: teamim,
);
for (var i = 0; i < shown.length; i++) {
final controller = PluginCorrectionController(
sample,
removeNikud: nikud,
removeTeamim: teamim,
);
addTearDown(controller.dispose);
expect(controller.text, shown);
controller.value = TextEditingValue(
text: shown.replaceRange(i, i + 1, ''),
selection: TextSelection.collapsed(offset: i),
);
// מה שהעורך מציג אחרי המחיקה הוא מה שהקורא יציג מהמקור שנשמר.
expect(
utils.removeMarks(
controller.canonical,
nikud: nikud,
teamim: teamim,
),
controller.text,
reason: 'מחיקת התו $i',
);
}
});
}
}
}

test('עריכה בלי סימון בחירה מעגנת את הטווח בסמן החדש', () {
final controller = PluginCorrectionController(
'ב$patahב$holam',
removeNikud: true,
);
addTearDown(controller.dispose);
controller.value = const TextEditingValue(
text: 'ב',
selection: TextSelection.collapsed(offset: 0),
);
expect(controller.canonical, 'ב$holam');
});

test('ערך מההיסטוריה שנוצר לפני החלפת ההסתרה מוצג לפי ההסתרה הנוכחית', () {
final controller = PluginCorrectionController('ב$patahב$holam');
addTearDown(controller.dispose);
final before = controller.value;
controller.value = before.copyWith(
text: 'ב$patahג$holam',
selection: const TextSelection.collapsed(offset: 3),
);
controller.setHiding(nikud: true, teamim: false);
expect(controller.text, 'בג');
controller.value = before;
expect(controller.text, 'בב');
expect(controller.canonical, 'ב$patahב$holam');
expect(controller.formats.length, 2);
});

test('החלפת ההסתרה ממפה את הבחירה ושומרת על העיצוב והטקסט המלא', () {
final controller = PluginCorrectionController(
'<b>ב$patah</b>ב$holam',
);
addTearDown(controller.dispose);
controller.selection = const TextSelection.collapsed(offset: 2);
controller.setHiding(nikud: true, teamim: false);
expect(controller.text, 'בב');
expect(controller.selection, const TextSelection.collapsed(offset: 1));
expect(controller.formats.map((f) => f.bold), [true, false]);
controller.setHiding(nikud: false, teamim: false);
expect(controller.text, 'ב$patahב$holam');
expect(controller.formats.map((f) => f.bold), [true, true, false, false]);
expect(controller.canonical, 'ב$patahב$holam');
});

test('שחזור טיוטה מציג אותה לפי ההסתרה ומשמר את הסימנים שבה', () {
final controller = PluginCorrectionController(
'ב$patahב$holamג',
removeNikud: true,
);
addTearDown(controller.dispose);
controller.restoreCanonical('ב$patahב');
expect(controller.text, 'בב');
expect(controller.canonical, 'ב$patahב');
});
});
}
Loading
Loading