Skip to content

Commit 876df41

Browse files
committed
PDF: שימור חיפוש מבחירה ושימוש חוזר בטקסט שהועתק
1 parent ad48a8d commit 876df41

3 files changed

Lines changed: 169 additions & 49 deletions

File tree

‎lib/pdf_book/utils/pdf_text_selection_actions.dart‎

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,29 @@ class PdfTextSelectionActions {
99
final PdfViewerController controller;
1010
final bool Function() isActive;
1111
int _generation = 0;
12+
({String text, bool Function() selectionIsCurrent})? _snapshot;
13+
14+
String? get selectedTextSnapshot {
15+
discardStaleSnapshot();
16+
return _snapshot?.text;
17+
}
18+
19+
void discardStaleSnapshot() {
20+
if (_snapshot?.selectionIsCurrent() == false) _snapshot = null;
21+
}
22+
23+
void releaseSelectedTextSnapshot() => _snapshot = null;
1224

1325
void cancel() => _generation++;
1426

1527
bool _isCurrent(
1628
int generation,
1729
PdfDocument document,
1830
PdfTextSelectionRange? range,
19-
) {
20-
if (generation != _generation ||
21-
!isActive() ||
31+
) => generation == _generation && _matchesSelection(document, range);
32+
33+
bool _matchesSelection(PdfDocument document, PdfTextSelectionRange? range) {
34+
if (!isActive() ||
2235
!controller.isReady ||
2336
!identical(controller.document, document)) {
2437
return false;
@@ -31,28 +44,23 @@ class PdfTextSelectionActions {
3144
}
3245

3346
bool Function() _selectedTextGuard(
34-
int generation,
3547
PdfDocument document,
3648
PdfTextSelectionRange range,
3749
) {
38-
final originalPages = document.pages;
39-
var checkedPages = originalPages;
50+
var checkedPages = document.pages;
4051
final start = range.start.text.pageNumber - 1;
4152
final end = range.end.text.pageNumber - 1;
4253
return () {
43-
if (!_isCurrent(generation, document, range) ||
44-
!controller.textSelectionDelegate.isCopyAllowed) {
45-
return false;
46-
}
54+
if (!_matchesSelection(document, range)) return false;
4755
final currentPages = document.pages;
4856
if (start < 0 ||
4957
end >= currentPages.length ||
50-
end >= originalPages.length) {
58+
end >= checkedPages.length) {
5159
return false;
5260
}
5361
if (!identical(currentPages, checkedPages)) {
5462
for (var i = start; i <= end; i++) {
55-
if (!identical(currentPages[i], originalPages[i])) return false;
63+
if (!identical(currentPages[i], checkedPages[i])) return false;
5664
}
5765
checkedPages = currentPages;
5866
}
@@ -69,7 +77,11 @@ class PdfTextSelectionActions {
6977
if (range == null || !selection.isCopyAllowed) {
7078
return PdfTextActionResult.none;
7179
}
72-
final isCurrent = _selectedTextGuard(generation, document, range);
80+
final selectionIsCurrent = _selectedTextGuard(document, range);
81+
bool isCurrent() =>
82+
generation == _generation &&
83+
selectionIsCurrent() &&
84+
selection.isCopyAllowed;
7385
try {
7486
if (!isCurrent()) return PdfTextActionResult.none;
7587
if (document.pages
@@ -78,7 +90,12 @@ class PdfTextSelectionActions {
7890
.any((page) => !page.isLoaded)) {
7991
return PdfTextActionResult.loading;
8092
}
81-
final text = await _readText(document, range, isCurrent);
93+
final text = await _readText(
94+
document,
95+
range,
96+
isCurrent: isCurrent,
97+
selectionIsCurrent: selectionIsCurrent,
98+
);
8299
if (!isCurrent() || text == null || text.isEmpty) {
83100
return PdfTextActionResult.none;
84101
}
@@ -87,6 +104,7 @@ class PdfTextSelectionActions {
87104
if (!isCurrent()) return PdfTextActionResult.none;
88105
if (!keepSelection) {
89106
await selection.clearTextSelection();
107+
discardStaleSnapshot();
90108
if (!_isCurrent(generation, document, null)) {
91109
return PdfTextActionResult.none;
92110
}
@@ -100,43 +118,67 @@ class PdfTextSelectionActions {
100118
}
101119

102120
/// קורא את הבחירה בלי להכניס גאומטריה של עמודי ביניים למטמון המשותף.
103-
Future<String> readSelectedText() async {
121+
Future<String?> readSelectedText({bool requireCopyPermission = true}) async {
104122
final generation = ++_generation;
105-
if (!isActive() || !controller.isReady) return '';
123+
if (!isActive() || !controller.isReady) return null;
106124
final document = controller.document;
107125
final selection = controller.textSelectionDelegate;
108126
final range = selection.textSelectionPointRange;
109-
if (range == null || !selection.isCopyAllowed) return '';
110-
final isCurrent = _selectedTextGuard(generation, document, range);
127+
if (range == null || (requireCopyPermission && !selection.isCopyAllowed)) {
128+
return null;
129+
}
130+
final selectionIsCurrent = _selectedTextGuard(document, range);
131+
bool isCurrent() =>
132+
generation == _generation &&
133+
selectionIsCurrent() &&
134+
(!requireCopyPermission || selection.isCopyAllowed);
111135
try {
112-
final text = await _readText(document, range, isCurrent);
113-
return isCurrent() ? text ?? '' : '';
136+
final text = await _readText(
137+
document,
138+
range,
139+
isCurrent: isCurrent,
140+
selectionIsCurrent: selectionIsCurrent,
141+
skipUnloadedPages: true,
142+
);
143+
return isCurrent() ? text : null;
114144
} catch (_) {
115-
if (!isCurrent()) return '';
145+
if (!isCurrent()) return null;
116146
rethrow;
117147
}
118148
}
119149

120150
Future<String?> _readText(
121151
PdfDocument document,
122-
PdfTextSelectionRange range,
123-
bool Function() isCurrent,
124-
) async {
152+
PdfTextSelectionRange range, {
153+
required bool Function() isCurrent,
154+
required bool Function() selectionIsCurrent,
155+
bool skipUnloadedPages = false,
156+
}) async {
157+
if (!isCurrent()) return null;
158+
final snapshot = selectedTextSnapshot;
159+
if (snapshot != null) return snapshot;
125160
final first = range.start;
126161
final last = range.end;
127162
final pages = document.pages;
128163
if (first.text.pageNumber < 1 || last.text.pageNumber > pages.length) {
129164
return null;
130165
}
131166
final result = StringBuffer();
167+
var complete = true;
132168
for (
133169
var number = first.text.pageNumber;
134170
number <= last.text.pageNumber;
135171
number++
136172
) {
137173
if (!isCurrent()) return null;
138174
final page = pages[number - 1];
139-
if (!page.isLoaded) return null;
175+
if (!page.isLoaded &&
176+
number != first.text.pageNumber &&
177+
number != last.text.pageNumber) {
178+
if (!skipUnloadedPages) return null;
179+
complete = false;
180+
continue;
181+
}
140182
// cache.get אינו מאמת זהות עמוד לפני שאירוע החלפת התוכן מופץ.
141183
final text = number == first.text.pageNumber
142184
? first.text
@@ -158,7 +200,11 @@ class PdfTextSelectionActions {
158200
.text,
159201
);
160202
}
161-
return result.toString();
203+
final text = result.toString();
204+
if (complete) {
205+
_snapshot = (text: text, selectionIsCurrent: selectionIsCurrent);
206+
}
207+
return text;
162208
}
163209

164210
Future<PdfTextActionResult> selectAll() async {

‎lib/pdf_book/view/pdf_book_screen.dart‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,12 @@ class _PdfBookScreenState extends State<PdfBookScreen>
11171117

11181118
final hasTextSelection = _hasPdfTextSelection();
11191119
final selectedText = hasTextSelection
1120-
? widget
1121-
.tab
1122-
.pdfViewerController
1123-
.textSelectionDelegate
1124-
.selectedTextIfLoaded
1120+
? _pdfTextSelectionActions.selectedTextSnapshot ??
1121+
widget
1122+
.tab
1123+
.pdfViewerController
1124+
.textSelectionDelegate
1125+
.selectedTextIfLoaded
11251126
: null;
11261127
final selection = selectedText == null
11271128
? null
@@ -1175,7 +1176,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
11751176
currentRef: pdfPageRef(widget.tab, pageNumber),
11761177
hasSelection: hasSelection,
11771178
loadedText: loadedText,
1178-
readSelectedText: _readPdfTextSelection,
1179+
readSelectedText: () async => await _readPdfTextSelection() ?? '',
11791180
copyAllowed: _isPdfCopyAllowed(),
11801181
menuContext: menuContext,
11811182
);
@@ -1219,17 +1220,24 @@ class _PdfBookScreenState extends State<PdfBookScreen>
12191220
errorMessage: CommonMessages.textCopyError,
12201221
);
12211222

1222-
Future<String> _readPdfTextSelection() async {
1223+
Future<String?> _readPdfTextSelection({
1224+
bool requireCopyPermission = true,
1225+
}) async {
12231226
try {
1224-
return await _pdfTextSelectionActions.readSelectedText();
1227+
return await _pdfTextSelectionActions.readSelectedText(
1228+
requireCopyPermission: requireCopyPermission,
1229+
);
12251230
} catch (_) {
12261231
if (mounted && _isPdfVisible) {
12271232
UiSnack.showError(PdfMessages.textSelectionFailed);
12281233
}
1229-
return '';
1234+
return null;
12301235
}
12311236
}
12321237

1238+
void _onPdfTextSelectionChanged(PdfTextSelection _) =>
1239+
_pdfTextSelectionActions.discardStaleSnapshot();
1240+
12331241
Future<void> _selectAllPdfText() => _showPdfTextActionResult(
12341242
_pdfTextSelectionActions.selectAll(),
12351243
errorMessage: PdfMessages.textSelectionFailed,
@@ -1311,13 +1319,18 @@ class _PdfBookScreenState extends State<PdfBookScreen>
13111319
) async {
13121320
final controller = widget.tab.pdfViewerController;
13131321
if (!controller.isReady) return;
1314-
final selection =
1315-
loadedSelection ??
1316-
ReaderMenuSelection(
1317-
await _readPdfTextSelection(),
1318-
);
1319-
if (!mounted || !_isPdfVisible || selection.cleaned.isEmpty) return;
1320-
openGlobalSearch(context, selection.cleaned, insertAdjacent: true);
1322+
final selection = loadedSelection;
1323+
final rawText = selection == null
1324+
? await _readPdfTextSelection(requireCopyPermission: false)
1325+
: null;
1326+
if (!mounted || !_isPdfVisible || (selection == null && rawText == null)) {
1327+
return;
1328+
}
1329+
openGlobalSearch(
1330+
context,
1331+
(selection ?? ReaderMenuSelection(rawText)).cleaned,
1332+
insertAdjacent: true,
1333+
);
13211334
}
13221335

13231336
/// צבע הרקע שמועבר ל-[PdfViewerParams.backgroundColor].
@@ -1376,6 +1389,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
13761389
scrollByMouseWheel: 1.0,
13771390
textSelectionParams: PdfTextSelectionParams(
13781391
enabled: !_isHandMode && !_isChoosingPersonalNotePoint,
1392+
onTextSelectionChange: _onPdfTextSelectionChanged,
13791393
),
13801394
// נעילת הציר מתבצעת ב-TrackpadPanRecognizer; PanAxis נועל גם אלכסונים.
13811395
panAxis: PanAxis.free,
@@ -4185,6 +4199,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
41854199

41864200
@override
41874201
void didHaveMemoryPressure() {
4202+
_pdfTextSelectionActions.releaseSelectedTextSnapshot();
41884203
_pageTurnController.stop(canceled: false);
41894204
_abortInteractivePageTurn();
41904205
_pendingPageTurns.clear();
@@ -4199,6 +4214,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
41994214

42004215
void _releaseImagesWhileHidden() {
42014216
_pdfTextSelectionActions.cancel();
4217+
_pdfTextSelectionActions.releaseSelectedTextSnapshot();
42024218
_trackpadFling.stop();
42034219
_scrollPhysicsProvider.stop();
42044220
_stopContinuousScroll();

0 commit comments

Comments
 (0)