@@ -48,6 +48,7 @@ const [zenModeWords, setZenModeWords] = createSignal<string[]>([])
48
48
const [ cardDisabledInZenMode , setCardDisabledInZenMode ] = createSignal ( false )
49
49
const [ curContextText , setCurContextText ] = createSignal ( '' )
50
50
const [ tabIndex , setTabIndex ] = createSignal ( 0 )
51
+ const [ isEditingContext , setIsEditingContext ] = createSignal ( false )
51
52
52
53
export const WhCard = customElement ( 'wh-card' , ( ) => {
53
54
const dictTabs = ( ) => settings ( ) [ 'dictTabs' ]
@@ -159,7 +160,7 @@ export const WhCard = customElement('wh-card', () => {
159
160
const onKeydown = ( e : KeyboardEvent ) => {
160
161
const cardNode = getCardNode ( )
161
162
const container = cardNode . querySelector ( '.dict_container' ) !
162
- if ( isCardVisible ( ) ) {
163
+ if ( isCardVisible ( ) && ! isEditingContext ( ) ) {
163
164
if ( ! e . altKey && ! e . metaKey && ! e . ctrlKey && ! e . shiftKey ) {
164
165
if ( e . key === 'ArrowUp' || e . key === 'ArrowDown' ) {
165
166
const selector = getDictAdapter ( ) . sectionSelector
@@ -386,6 +387,57 @@ export function ZenMode() {
386
387
387
388
function ContextList ( props : { contexts : WordContext [ ] } ) {
388
389
const allTensionWords = ( ) => getWordAllTenses ( curWord ( ) ) . reverse ( )
390
+ const [ editingContext , setEditingContext ] = createSignal < WordContext | null > ( null )
391
+ const [ prevText , setPrevText ] = createSignal < string > ( '' )
392
+ let editRef : HTMLDivElement | undefined
393
+
394
+ function enterEditMode ( context : WordContext ) {
395
+ setIsEditingContext ( true )
396
+ setEditingContext ( context )
397
+ setPrevText ( context . text )
398
+ }
399
+
400
+ function exitEditMode ( ) {
401
+ setIsEditingContext ( false )
402
+ setEditingContext ( null )
403
+ setPrevText ( '' )
404
+ }
405
+
406
+ function updateContextText ( ) {
407
+ const currentContext = editingContext ( )
408
+
409
+ if ( ! editRef || ! currentContext ) {
410
+ exitEditMode ( )
411
+ return
412
+ }
413
+ currentContext . text = ( editRef . textContent ?? '' ) . trim ( )
414
+ }
415
+
416
+ function saveContext ( ) {
417
+ const currentContext = editingContext ( )
418
+
419
+ if ( ! currentContext ) {
420
+ exitEditMode ( )
421
+ return
422
+ }
423
+
424
+ updateContextText ( )
425
+ deleteContext ( {
426
+ ...currentContext ,
427
+ text : prevText ( ) ,
428
+ timestamp : Date . now ( )
429
+ } )
430
+ addContext ( currentContext . word , currentContext . text )
431
+ exitEditMode ( )
432
+ }
433
+
434
+ function updateAndFocusEl ( el : HTMLDivElement ) {
435
+ editRef = el
436
+ setTimeout ( ( ) => {
437
+ editRef ?. focus ( )
438
+ } )
439
+ }
440
+
389
441
return (
390
442
< Show
391
443
when = { props . contexts . length > 0 }
@@ -405,16 +457,33 @@ function ContextList(props: { contexts: WordContext[] }) {
405
457
link = link . replaceAll ( '-' , '%2D' )
406
458
return (
407
459
< div >
408
- < pre innerHTML = { highlightedContext } > </ pre >
460
+ < Show when = { editingContext ( ) === context } >
461
+ < div
462
+ ref = { el => updateAndFocusEl ( el ) }
463
+ onblur = { ( ) => saveContext ( ) }
464
+ oninput = { ( ) => updateContextText ( ) }
465
+ contenteditable
466
+ >
467
+ { context . text }
468
+ </ div >
469
+ </ Show >
470
+ < Show when = { editingContext ( ) !== context } >
471
+ < pre innerHTML = { highlightedContext } > </ pre >
472
+ </ Show >
409
473
< p >
410
474
< img src = { context . favicon || getFaviconByDomain ( context . url ) } alt = "favicon" />
411
475
< a href = { link } target = "_blank" >
412
476
{ context . title }
413
477
</ a >
414
478
</ p >
415
- < button title = "delete context" onclick = { ( ) => deleteContext ( context ) } >
416
- < img src = { chrome . runtime . getURL ( 'icons/cancel.png' ) } alt = "delete" />
417
- </ button >
479
+ < div class = "flex items-center absolute top-5 right-0 gap-4" >
480
+ < button title = "edit context" onClick = { ( ) => enterEditMode ( context ) } >
481
+ < img src = { chrome . runtime . getURL ( 'icons/edit.png' ) } alt = "edit" />
482
+ </ button >
483
+ < button title = "delete context" onClick = { ( ) => deleteContext ( context ) } >
484
+ < img src = { chrome . runtime . getURL ( 'icons/cancel.png' ) } alt = "delete" />
485
+ </ button >
486
+ </ div >
418
487
</ div >
419
488
)
420
489
} }
0 commit comments