@@ -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 {
0 commit comments