1+ import 'dart:async' ;
2+
13import 'package:flutter/services.dart' ;
24import 'package:pdfrx/pdfrx.dart' ;
35
@@ -9,16 +11,34 @@ class PdfTextSelectionActions {
911 final PdfViewerController controller;
1012 final bool Function () isActive;
1113 int _generation = 0 ;
14+ ({String text, bool Function () selectionIsCurrent})? _snapshot;
15+ StreamSubscription <PdfDocumentEvent >? _snapshotEvents;
16+
17+ String ? get selectedTextSnapshot {
18+ discardStaleSnapshot ();
19+ return _snapshot? .text;
20+ }
21+
22+ void discardStaleSnapshot () {
23+ if (_snapshot? .selectionIsCurrent () == false ) releaseSelectedTextSnapshot ();
24+ }
25+
26+ void releaseSelectedTextSnapshot () {
27+ _snapshot = null ;
28+ unawaited (_snapshotEvents? .cancel ());
29+ _snapshotEvents = null ;
30+ }
1231
1332 void cancel () => _generation++ ;
1433
1534 bool _isCurrent (
1635 int generation,
1736 PdfDocument document,
1837 PdfTextSelectionRange ? range,
19- ) {
20- if (generation != _generation ||
21- ! isActive () ||
38+ ) => generation == _generation && _matchesSelection (document, range);
39+
40+ bool _matchesSelection (PdfDocument document, PdfTextSelectionRange ? range) {
41+ if (! isActive () ||
2242 ! controller.isReady ||
2343 ! identical (controller.document, document)) {
2444 return false ;
@@ -30,6 +50,31 @@ class PdfTextSelectionActions {
3050 current? .end == range? .end;
3151 }
3252
53+ bool Function () _selectedTextGuard (
54+ PdfDocument document,
55+ PdfTextSelectionRange range,
56+ ) {
57+ var checkedPages = document.pages;
58+ final start = range.start.text.pageNumber - 1 ;
59+ final end = range.end.text.pageNumber - 1 ;
60+ return () {
61+ if (! _matchesSelection (document, range)) return false ;
62+ final currentPages = document.pages;
63+ if (start < 0 ||
64+ end >= currentPages.length ||
65+ end >= checkedPages.length) {
66+ return false ;
67+ }
68+ if (! identical (currentPages, checkedPages)) {
69+ for (var i = start; i <= end; i++ ) {
70+ if (! identical (currentPages[i], checkedPages[i])) return false ;
71+ }
72+ checkedPages = currentPages;
73+ }
74+ return true ;
75+ };
76+ }
77+
3378 Future <PdfTextActionResult > copy ({bool keepSelection = false }) async {
3479 final generation = ++ _generation;
3580 if (! isActive () || ! controller.isReady) return PdfTextActionResult .none;
@@ -39,7 +84,11 @@ class PdfTextSelectionActions {
3984 if (range == null || ! selection.isCopyAllowed) {
4085 return PdfTextActionResult .none;
4186 }
42- bool isCurrent () => _isCurrent (generation, document, range);
87+ final selectionIsCurrent = _selectedTextGuard (document, range);
88+ bool isCurrent () =>
89+ generation == _generation &&
90+ selectionIsCurrent () &&
91+ selection.isCopyAllowed;
4392 try {
4493 if (! isCurrent ()) return PdfTextActionResult .none;
4594 if (document.pages
@@ -48,15 +97,21 @@ class PdfTextSelectionActions {
4897 .any ((page) => ! page.isLoaded)) {
4998 return PdfTextActionResult .loading;
5099 }
51- final text = await selection.getSelectedText ();
52- if (! isCurrent () || ! selection.isCopyAllowed || text.isEmpty) {
100+ final text = await _readText (
101+ document,
102+ range,
103+ isCurrent: isCurrent,
104+ selectionIsCurrent: selectionIsCurrent,
105+ );
106+ if (! isCurrent () || text == null || text.isEmpty) {
53107 return PdfTextActionResult .none;
54108 }
55109 // pdfrx's copy helper does not await the platform clipboard response.
56110 await Clipboard .setData (ClipboardData (text: text));
57111 if (! isCurrent ()) return PdfTextActionResult .none;
58112 if (! keepSelection) {
59113 await selection.clearTextSelection ();
114+ discardStaleSnapshot ();
60115 if (! _isCurrent (generation, document, null )) {
61116 return PdfTextActionResult .none;
62117 }
@@ -69,6 +124,102 @@ class PdfTextSelectionActions {
69124 }
70125 }
71126
127+ /// קורא את הבחירה בלי להכניס גאומטריה של עמודי ביניים למטמון המשותף.
128+ Future <String ?> readSelectedText ({bool requireCopyPermission = true }) async {
129+ final generation = ++ _generation;
130+ if (! isActive () || ! controller.isReady) return null ;
131+ final document = controller.document;
132+ final selection = controller.textSelectionDelegate;
133+ final range = selection.textSelectionPointRange;
134+ if (range == null || (requireCopyPermission && ! selection.isCopyAllowed)) {
135+ return null ;
136+ }
137+ final selectionIsCurrent = _selectedTextGuard (document, range);
138+ bool isCurrent () =>
139+ generation == _generation &&
140+ selectionIsCurrent () &&
141+ (! requireCopyPermission || selection.isCopyAllowed);
142+ try {
143+ final text = await _readText (
144+ document,
145+ range,
146+ isCurrent: isCurrent,
147+ selectionIsCurrent: selectionIsCurrent,
148+ skipUnloadedPages: true ,
149+ );
150+ return isCurrent () ? text : null ;
151+ } catch (_) {
152+ if (! isCurrent ()) return null ;
153+ rethrow ;
154+ }
155+ }
156+
157+ Future <String ?> _readText (
158+ PdfDocument document,
159+ PdfTextSelectionRange range, {
160+ required bool Function () isCurrent,
161+ required bool Function () selectionIsCurrent,
162+ bool skipUnloadedPages = false ,
163+ }) async {
164+ if (! isCurrent ()) return null ;
165+ final snapshot = selectedTextSnapshot;
166+ if (snapshot != null ) return snapshot;
167+ final first = range.start;
168+ final last = range.end;
169+ final pages = document.pages;
170+ if (first.text.pageNumber < 1 || last.text.pageNumber > pages.length) {
171+ return null ;
172+ }
173+ final result = StringBuffer ();
174+ var complete = true ;
175+ for (
176+ var number = first.text.pageNumber;
177+ number <= last.text.pageNumber;
178+ number++
179+ ) {
180+ if (! isCurrent ()) return null ;
181+ final page = pages[number - 1 ];
182+ if (! page.isLoaded &&
183+ number != first.text.pageNumber &&
184+ number != last.text.pageNumber) {
185+ if (! skipUnloadedPages) return null ;
186+ complete = false ;
187+ continue ;
188+ }
189+ // cache.get אינו מאמת זהות עמוד לפני שאירוע החלפת התוכן מופץ.
190+ final text = number == first.text.pageNumber
191+ ? first.text
192+ : number == last.text.pageNumber
193+ ? last.text
194+ : await page.loadStructuredText (ensureLoaded: false );
195+ if (! isCurrent () || ! identical (document.pages[number - 1 ], page)) {
196+ return null ;
197+ }
198+ if (text.fullText.isEmpty) continue ;
199+ result.write (
200+ text
201+ .getRangeFromAB (
202+ number == first.text.pageNumber ? first.index : 0 ,
203+ number == last.text.pageNumber
204+ ? last.index
205+ : text.charRects.length - 1 ,
206+ )
207+ .text,
208+ );
209+ }
210+ final text = result.toString ();
211+ if (complete) {
212+ releaseSelectedTextSnapshot ();
213+ _snapshot = (text: text, selectionIsCurrent: selectionIsCurrent);
214+ // ניקוי בחירה בעקבות אירועי מסמך אינו תמיד מפעיל onTextSelectionChange.
215+ _snapshotEvents = document.events.listen (
216+ (_) => discardStaleSnapshot (),
217+ onDone: releaseSelectedTextSnapshot,
218+ );
219+ }
220+ return text;
221+ }
222+
72223 Future <PdfTextActionResult > selectAll () async {
73224 final generation = ++ _generation;
74225 if (! isActive () || ! controller.isReady) return PdfTextActionResult .none;
@@ -105,8 +256,8 @@ class PdfTextSelectionActions {
105256 if (document.pages.any ((page) => ! page.isLoaded)) {
106257 return PdfTextActionResult .loading;
107258 }
108- // This setter applies synchronously; native selectAllText awaits without
109- // checking whether the viewer or its selection has changed in the meantime .
259+ // ה- setter סינכרוני; selectAllText של החבילה ממתין בלי לבדוק
260+ // אם הצפיין או הבחירה הוחלפו בזמן ההמתנה .
110261 await selection.setTextSelectionPointRange (
111262 PdfTextSelectionRange .fromPoints (
112263 PdfTextSelectionPoint (first, 0 ),
0 commit comments