@@ -226,11 +226,8 @@ function recordMouseEvent(what, evt, canvas, display, options) {
226
226
display . signalInputEvent ( ) ;
227
227
}
228
228
display . idle = 0 ;
229
- if ( what == 'mouseup' ) {
230
- if ( display . runFor ) display . runFor ( 100 ) ; // maybe we catch the fullscreen flag change
231
- } else {
232
- if ( display . runNow ) display . runNow ( ) ; // don't wait for timeout to run
233
- }
229
+ if ( what === 'mouseup' ) display . runFor ( 100 , what ) ; // process copy/paste or fullscreen flag change
230
+ else display . runNow ( what ) ; // don't wait for timeout to run
234
231
}
235
232
236
233
function recordWheelEvent ( evt , display ) {
@@ -253,6 +250,7 @@ function recordWheelEvent(evt, display) {
253
250
if ( display . signalInputEvent )
254
251
display . signalInputEvent ( ) ;
255
252
display . idle = 0 ;
253
+ if ( display . runNow ) display . runNow ( 'wheel' ) ; // don't wait for timeout to run
256
254
}
257
255
258
256
// Squeak traditional keycodes are MacRoman
@@ -304,7 +302,7 @@ function recordKeyboardEvent(unicode, timestamp, display) {
304
302
display . keys . push ( modifiersAndKey ) ;
305
303
}
306
304
display . idle = 0 ;
307
- if ( display . runNow ) display . runNow ( ) ; // don't wait for timeout to run
305
+ if ( display . runNow ) display . runNow ( 'keyboard' ) ; // don't wait for timeout to run
308
306
}
309
307
310
308
function recordDragDropEvent ( type , evt , canvas , display ) {
@@ -321,6 +319,8 @@ function recordDragDropEvent(type, evt, canvas, display) {
321
319
] ) ;
322
320
if ( display . signalInputEvent )
323
321
display . signalInputEvent ( ) ;
322
+ display . idle = 0 ;
323
+ if ( display . runNow ) display . runNow ( 'drag-drop' ) ; // don't wait for timeout to run
324
324
}
325
325
326
326
function fakeCmdOrCtrlKey ( key , timestamp , display ) {
@@ -360,6 +360,7 @@ function createSqueakDisplay(canvas, options) {
360
360
eventQueue : null , // only used if image uses event primitives
361
361
clipboardString : '' ,
362
362
clipboardStringChanged : false ,
363
+ handlingEvent : '' , // set to 'mouse' or 'keyboard' while handling an event
363
364
cursorCanvas : options . cursor !== false && document . getElementById ( "sqCursor" ) || document . createElement ( "canvas" ) ,
364
365
cursorOffsetX : 0 ,
365
366
cursorOffsetY : 0 ,
@@ -446,7 +447,7 @@ function createSqueakDisplay(canvas, options) {
446
447
ctx . fillStyle = style . color || "#F90" ;
447
448
ctx . fillRect ( x , y , w * value , h ) ;
448
449
} ;
449
- display . executeClipboardPaste = function ( text , timestamp ) {
450
+ display . executeClipboardPasteKey = function ( text , timestamp ) {
450
451
if ( ! display . vm ) return true ;
451
452
try {
452
453
display . clipboardString = text ;
@@ -456,7 +457,7 @@ function createSqueakDisplay(canvas, options) {
456
457
console . error ( "paste error " + err ) ;
457
458
}
458
459
} ;
459
- display . executeClipboardCopy = function ( key , timestamp ) {
460
+ display . executeClipboardCopyKey = function ( key , timestamp ) {
460
461
if ( ! display . vm ) return true ;
461
462
// simulate copy event for Squeak so it places its text in clipboard
462
463
display . clipboardStringChanged = false ;
@@ -863,18 +864,18 @@ function createSqueakDisplay(canvas, options) {
863
864
switch ( evt . key ) {
864
865
case "c" :
865
866
case "x" :
866
- if ( ! navigator . clipboard ?. writeText ) return ; // fire document.oncopy/oncut
867
- var text = display . executeClipboardCopy ( evt . key , evt . timeStamp ) ;
867
+ if ( ! navigator . clipboard ) return ; // fire document.oncopy/oncut
868
+ var text = display . executeClipboardCopyKey ( evt . key , evt . timeStamp ) ;
868
869
if ( typeof text === 'string' ) {
869
870
navigator . clipboard . writeText ( text )
870
871
. catch ( function ( err ) { console . error ( "display: copy error " + err . message ) ; } ) ;
871
872
}
872
873
return evt . preventDefault ( ) ;
873
874
case "v" :
874
- if ( ! navigator . clipboard ?. readText ) return ; // fire document.onpaste
875
+ if ( ! navigator . clipboard ) return ; // fire document.onpaste
875
876
navigator . clipboard . readText ( )
876
877
. then ( function ( text ) {
877
- display . executeClipboardPaste ( text , evt . timeStamp ) ;
878
+ display . executeClipboardPasteKey ( text , evt . timeStamp ) ;
878
879
} )
879
880
. catch ( function ( err ) { console . error ( "display: paste error " + err . message ) ; } ) ;
880
881
return evt . preventDefault ( ) ;
@@ -892,10 +893,25 @@ function createSqueakDisplay(canvas, options) {
892
893
if ( ! display . vm ) return true ;
893
894
recordModifiers ( evt , display ) ;
894
895
} ;
895
- // copy/paste old-style
896
- if ( ! navigator . clipboard ?. writeText ) {
896
+ // more copy/paste
897
+ if ( navigator . clipboard ) {
898
+ // new-style copy/paste (all modern browsers)
899
+ display . readFromSystemClipboard = ( ) => navigator . clipboard . readText ( )
900
+ . then ( text => display . clipboardString = text )
901
+ . catch ( err => {
902
+ if ( ! display . handlingEvent ) console . warn ( "reading from clipboard outside event handler" ) ;
903
+ console . error ( "readFromSystemClipboard" + err . message ) ;
904
+ } ) ;
905
+ display . writeToSystemClipboard = ( ) => navigator . clipboard . writeText ( display . clipboardString )
906
+ . then ( ( ) => display . clipboardStringChanged = false )
907
+ . catch ( err => {
908
+ if ( ! display . handlingEvent ) console . warn ( "writing to clipboard outside event handler" ) ;
909
+ console . error ( "writeToSystemClipboard" + err . message ) ;
910
+ } ) ;
911
+ } else {
912
+ // old-style copy/paste
897
913
document . oncopy = function ( evt , key ) {
898
- var text = display . executeClipboardCopy ( key , evt . timeStamp ) ;
914
+ var text = display . executeClipboardCopyKey ( key , evt . timeStamp ) ;
899
915
if ( typeof text === 'string' ) {
900
916
evt . clipboardData . setData ( "Text" , text ) ;
901
917
}
@@ -905,11 +921,9 @@ function createSqueakDisplay(canvas, options) {
905
921
if ( ! display . vm ) return true ;
906
922
document . oncopy ( evt , 'x' ) ;
907
923
} ;
908
- }
909
- if ( ! navigator . clipboard ?. readText ) {
910
924
document . onpaste = function ( evt ) {
911
925
var text = evt . clipboardData . getData ( 'Text' ) ;
912
- display . executeClipboardPaste ( text , evt . timeStamp ) ;
926
+ display . executeClipboardPasteKey ( text , evt . timeStamp ) ;
913
927
evt . preventDefault ( ) ;
914
928
} ;
915
929
}
@@ -1113,15 +1127,17 @@ SqueakJS.runImage = function(buffer, name, display, options) {
1113
1127
alert ( error ) ;
1114
1128
}
1115
1129
}
1116
- display . runNow = function ( ) {
1130
+ display . runNow = function ( event ) {
1117
1131
window . clearTimeout ( loop ) ;
1132
+ display . handlingEvent = event ;
1118
1133
run ( ) ;
1134
+ display . handlingEvent = '' ;
1119
1135
} ;
1120
- display . runFor = function ( milliseconds ) {
1136
+ display . runFor = function ( milliseconds , event ) {
1121
1137
var stoptime = Date . now ( ) + milliseconds ;
1122
1138
do {
1123
1139
if ( display . quitFlag ) return ;
1124
- display . runNow ( ) ;
1140
+ display . runNow ( event ) ;
1125
1141
} while ( Date . now ( ) < stoptime ) ;
1126
1142
} ;
1127
1143
if ( options . onStart ) options . onStart ( vm , display , options ) ;
0 commit comments