Skip to content

Commit b878191

Browse files
committed
feat(WIP): add tailwind directives; context edit;
1 parent 20a0b4f commit b878191

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

public/icons/edit.png

825 Bytes
Loading

src/content/card.css

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
15
.word_card {
26
--bg-color: #fff;
37
--border-color: rgba(0, 0, 0, 0.3);
@@ -195,9 +199,6 @@
195199

196200
button {
197201
opacity: 0;
198-
position: absolute;
199-
right: 0px;
200-
top: 5px;
201202
border: none;
202203
background: none;
203204
cursor: pointer;

src/content/card.tsx

+74-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const [zenModeWords, setZenModeWords] = createSignal<string[]>([])
4848
const [cardDisabledInZenMode, setCardDisabledInZenMode] = createSignal(false)
4949
const [curContextText, setCurContextText] = createSignal('')
5050
const [tabIndex, setTabIndex] = createSignal(0)
51+
const [isEditingContext, setIsEditingContext] = createSignal(false)
5152

5253
export const WhCard = customElement('wh-card', () => {
5354
const dictTabs = () => settings()['dictTabs']
@@ -159,7 +160,7 @@ export const WhCard = customElement('wh-card', () => {
159160
const onKeydown = (e: KeyboardEvent) => {
160161
const cardNode = getCardNode()
161162
const container = cardNode.querySelector('.dict_container')!
162-
if (isCardVisible()) {
163+
if (isCardVisible() && !isEditingContext()) {
163164
if (!e.altKey && !e.metaKey && !e.ctrlKey && !e.shiftKey) {
164165
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
165166
const selector = getDictAdapter().sectionSelector
@@ -386,6 +387,57 @@ export function ZenMode() {
386387

387388
function ContextList(props: { contexts: WordContext[] }) {
388389
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+
389441
return (
390442
<Show
391443
when={props.contexts.length > 0}
@@ -405,16 +457,33 @@ function ContextList(props: { contexts: WordContext[] }) {
405457
link = link.replaceAll('-', '%2D')
406458
return (
407459
<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>
409473
<p>
410474
<img src={context.favicon || getFaviconByDomain(context.url)} alt="favicon" />
411475
<a href={link} target="_blank">
412476
{context.title}
413477
</a>
414478
</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>
418487
</div>
419488
)
420489
}}

tailwind.config.cjs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
module.exports = {
33
content: ['./src/**/*.{js,jsx,ts,tsx}'],
44
theme: {
5-
extend: {}
5+
extend: {
6+
spacing: {
7+
1: '1px',
8+
2: '2px',
9+
3: '3px',
10+
4: '4px',
11+
5: '5px',
12+
6: '6px'
13+
}
14+
}
615
},
716
plugins: [require('tailwindcss/nesting'), require('daisyui')],
817
daisyui: {

0 commit comments

Comments
 (0)