generated from pengx17/logseq-plugin-template-react
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor with new hooks, and fix theme mode issue
- Loading branch information
Showing
7 changed files
with
139 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect } from "react"; | ||
import Mousetrap from 'mousetrap'; | ||
import 'mousetrap-global-bind'; | ||
|
||
export function useHotKey(hotkey: string) { | ||
useEffect(() => { | ||
if (!hotkey) { | ||
return; | ||
} | ||
|
||
// @ts-ignore | ||
Mousetrap.bindGlobal( | ||
hotkey, | ||
() => window.logseq.hideMainUI(), | ||
'keydown', | ||
); | ||
|
||
// @ts-ignore | ||
Mousetrap.bindGlobal( | ||
'Esc', | ||
() => window.logseq.hideMainUI(), | ||
'keydown', | ||
); | ||
|
||
return () => { | ||
// @ts-ignore | ||
Mousetrap.unbindGlobal(hotkey, 'keydown'); | ||
// @ts-ignore | ||
Mousetrap.unbindGlobal('Esc', 'keydown'); | ||
}; | ||
}, [hotkey]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useRecoilCallback } from "recoil"; | ||
|
||
export function useRefreshAll() { | ||
const refreshAll = useRecoilCallback( | ||
({ snapshot, refresh }) => | ||
() => { | ||
for (const node of snapshot.getNodes_UNSTABLE()) { | ||
refresh(node); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
return refreshAll; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
import { AppUserConfigs } from '@logseq/libs/dist/LSPlugin'; | ||
import { atom, AtomEffect } from 'recoil'; | ||
import { logseq as plugin } from '../../package.json'; | ||
|
||
const updateUserConfigsEffect: AtomEffect<Partial<AppUserConfigs>> = ({ | ||
setSelf, | ||
trigger, | ||
}) => { | ||
if (trigger === 'get') { | ||
window.logseq.App.getUserConfigs().then(setSelf); | ||
export const USER_CONFIGS_KEY = `${plugin.id}#userConfigs`; | ||
|
||
export const DEFAULT_USER_CONFIGS: Partial<AppUserConfigs> = { | ||
preferredLanguage: 'en', | ||
preferredThemeMode: 'light', | ||
preferredFormat: 'markdown', | ||
preferredWorkflow: 'now', | ||
preferredTodo: 'LATER', | ||
preferredDateFormat: 'MMM do, yyyy', | ||
}; | ||
|
||
const themeModeChangeEffect: AtomEffect<AppUserConfigs> = ({ onSet }) => { | ||
onSet(({ preferredThemeMode }) => { | ||
if (preferredThemeMode === 'dark') { | ||
document.documentElement.classList.add('dark'); | ||
document.documentElement.classList.remove('light'); | ||
} else { | ||
document.documentElement.classList.add('light'); | ||
document.documentElement.classList.remove('dark'); | ||
} | ||
}); | ||
}; | ||
|
||
const localStorageEffect: AtomEffect<AppUserConfigs> = ({ setSelf, onSet }) => { | ||
const savedValue = localStorage.getItem(USER_CONFIGS_KEY); | ||
if (savedValue != null) { | ||
setSelf(JSON.parse(savedValue)); | ||
} | ||
|
||
onSet((newValue, _, isReset) => { | ||
isReset | ||
? localStorage.removeItem(USER_CONFIGS_KEY) | ||
: localStorage.setItem(USER_CONFIGS_KEY, JSON.stringify(newValue)); | ||
}); | ||
}; | ||
|
||
export const userConfigsState = atom({ | ||
export const userConfigsState = atom<AppUserConfigs>({ | ||
key: 'userConfigs', | ||
default: () => window.logseq.App.getUserConfigs(), | ||
effects: [updateUserConfigsEffect], | ||
default: DEFAULT_USER_CONFIGS as AppUserConfigs, | ||
effects: [localStorageEffect, themeModeChangeEffect], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters