Skip to content

Commit

Permalink
fix: some focus bugs (#250)
Browse files Browse the repository at this point in the history
* fix: some focus bugs

* format
  • Loading branch information
nakasyou authored Nov 8, 2024
1 parent 56694af commit 7f53337
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 60 deletions.
11 changes: 0 additions & 11 deletions src/islands/note/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ export default (props: Props) => {
})
const [getLoadError, setLoadError] = createSignal<string>()

createEffect(() => {
if (!noteBookState.isEditMode) {
for (const note of notes.notes()) {
for (const handler of note.events?.focus ?? []) {
handler({
isActive: false,
})
}
}
}
})
createEffect(() => {
document.title = `${noteBookMetadata.noteName} - Nanoha`
})
Expand Down
22 changes: 1 addition & 21 deletions src/islands/note/components/Notes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ export default (props: {
notes.notes
handleUpdate()
})
createEffect(() => {
const focused = getFocusedIndex()
let i = 0
for (const eachNote of props.notes) {
const isActive = focused === i
for (const focusEventListener of eachNote.events.focus || []) {
focusEventListener({
isActive,
})
}
i++
}
})

const getNoteRect: Record<string, () => DOMRect> = {}

Expand Down Expand Up @@ -101,16 +88,9 @@ export default (props: {
setFocusedIndex(index())
}}
updated={handleUpdate}
on={(type, listener) => {
const thisNote = props.notes[index()]
if (!thisNote) return
if (!thisNote.events[type]) {
thisNote.events[type] = []
}
thisNote.events[type]?.push(listener)
}}
index={index()}
notes={props.notes}
focusedIndex={getFocusedIndex()}
/>
</div>
)
Expand Down
16 changes: 2 additions & 14 deletions src/islands/note/components/notes-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,23 @@ export interface NoteComponentProps<T extends MargedNoteData = MargedNoteData> {
setNoteData: SetStoreFunction<T>

focus(): void
on<EventType extends keyof NoteEvents>(
type: EventType,
listenter: (evt: NoteEventArgs[EventType]) => void,
): void

updated(): void

index: number
notes: Note[]

focusedIndex: number
}
export type NoteComponent<T extends MargedNoteData = MargedNoteData> = (
props: NoteComponentProps<T>,
) => JSX.Element

export interface NoteEvents {
focus?: ((evt: NoteEventArgs['focus']) => void)[]
}
export interface NoteEventArgs {
focus: {
isActive: boolean
}
}
export interface Note<T extends MargedNoteData = MargedNoteData> {
Component: NoteComponent<T>

noteData: T
setNoteData: SetStoreFunction<T>

events: NoteEvents
}

export const createNotes = (): {
Expand Down
5 changes: 1 addition & 4 deletions src/islands/note/components/notes/ImageNote/ImageNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import Player from './components/Player'
import { ScanedImageEditor } from './components/ScanedImageEditor'

export const ImageNote = ((props) => {
const [getIsActive, setIsActive] = createSignal(false)
const getIsActive = createMemo(() => props.focusedIndex === props.index)

props.on('focus', (evt) => {
setIsActive(evt.isActive)
})
const imageUrl = createMemo(() => {
const nowImageBlob = props.noteData.blobs.scanedImage
return nowImageBlob ? URL.createObjectURL(nowImageBlob) : null
Expand Down
1 change: 0 additions & 1 deletion src/islands/note/components/notes/ImageNote/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const createImageNote = (initNoteData?: ImageNoteData) => {
Component: ImageNote,
noteData,
setNoteData,
events: {},
}
return addNote
}
13 changes: 5 additions & 8 deletions src/islands/note/components/notes/TextNote/TextNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import { ExtensionPreviewLLM, ExtensionSheet } from './tiptap/plugins'
import type { TextNoteData } from './types'

import './TextNoteStyle.css'
import dedent from 'dedent'
import markdownIt from 'markdown-it'
import type { SetStoreFunction } from 'solid-js/store'
import { getGoogleGenerativeAI } from '../../../../shared/gemini'
import { getVisualViewport } from '../../../window-apis'
import { AIDialogCore } from './AI'

Expand Down Expand Up @@ -96,16 +92,16 @@ export const TextNote = ((props) => {
props.updated()
}

props.on('focus', (evt) => {
setIsActive(evt.isActive)
createEffect(() => {
setIsActive(props.focusedIndex === props.index)
})

let lastActive = getIsActive()
createEffect(() => {
if (getIsActive() && !lastActive) {
requestAnimationFrame(() => {
setTimeout(() => {
getEditor()?.commands.focus()
})
}, 100)
}
lastActive = getIsActive()
})
Expand Down Expand Up @@ -152,6 +148,7 @@ export const TextNote = ((props) => {
<AIDialogCore
close={(r) => {
getEditor()?.commands.insertContent(r)
saveContent()
close(null)
}}
initPrompt={(() => {
Expand Down
1 change: 0 additions & 1 deletion src/islands/note/components/notes/TextNote/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const createTextNote = (initNoteData?: TextNoteData) => {
Component: TextNote,
noteData,
setNoteData,
events: {},
}
return addNote
}

0 comments on commit 7f53337

Please sign in to comment.