-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Fix and enhance comment editor monospace toggle #36181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4f9c25b
Fix comment editor monospace toggle for edit mode
silverwind 8fcfbb7
rename
silverwind 439bf5a
update comment
silverwind f45185d
add storage event handling
silverwind f29f63a
rename var
silverwind 9f13c21
verify e.storageArea
silverwind f10ab8d
add minimal localStorage wrappers
silverwind f30f4a0
remove return value, not yet needed
silverwind 8dea163
Merge branch 'main' into mono
silverwind 6605d6f
Merge branch 'main' into mono
wxiaoguang b32c75f
fix
wxiaoguang 29b7236
fine tune
wxiaoguang 398c42c
fix format
wxiaoguang 45e5d49
fine tune error handling
wxiaoguang 2e75289
add generic
silverwind 17b6149
avoid null access
wxiaoguang 0b01a8f
getJsonObject<T> shouldn't accept null default value
wxiaoguang aa09638
add comment
wxiaoguang 1203c7b
add generic to setJsonObject
silverwind f214754
change prefix
wxiaoguang 62aa128
move applyMonospaceToAllEditors out from class, it doesn't belong to …
wxiaoguang 51a1ce5
add error log when JSON.parse fails
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| // Some people deploy Gitea under a subpath, so it needs prefix to avoid local storage key conflicts. | ||
| // And these keys are for user settings only, it also needs a specific prefix, | ||
| // in case in the future there are other uses of local storage, and/or we need to clear some keys when the quota is exceeded. | ||
| const itemKeyPrefix = 'gitea:setting:'; | ||
|
|
||
| function handleLocalStorageError(e: any) { | ||
| // in the future, maybe we need to handle quota exceeded errors differently | ||
| console.error('Error using local storage for user settings', e); | ||
| } | ||
|
|
||
| function getLocalStorageUserSetting(settingKey: string): string | null { | ||
| const legacyKey = settingKey; | ||
| const itemKey = `${itemKeyPrefix}${settingKey}`; | ||
| try { | ||
| const legacyValue = localStorage?.getItem(legacyKey) ?? null; | ||
| const value = localStorage?.getItem(itemKey) ?? null; // avoid undefined | ||
| if (value !== null && legacyValue !== null) { | ||
| // if both values exist, remove the legacy one | ||
| localStorage?.removeItem(legacyKey); | ||
| } else if (value === null && legacyValue !== null) { | ||
| // migrate legacy value to new key | ||
| localStorage?.removeItem(legacyKey); | ||
| localStorage?.setItem(itemKey, legacyValue); | ||
| return legacyValue; | ||
| } | ||
| return value; | ||
| } catch (e) { | ||
| handleLocalStorageError(e); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function setLocalStorageUserSetting(settingKey: string, value: string) { | ||
| const legacyKey = settingKey; | ||
| const itemKey = `${itemKeyPrefix}${settingKey}`; | ||
| try { | ||
| localStorage?.removeItem(legacyKey); | ||
| localStorage?.setItem(itemKey, value); | ||
| } catch (e) { | ||
| handleLocalStorageError(e); | ||
| } | ||
| } | ||
|
|
||
| export const localUserSettings = { | ||
| getString: (key: string, def: string = ''): string => { | ||
| return getLocalStorageUserSetting(key) ?? def; | ||
| }, | ||
| setString: (key: string, value: string) => { | ||
| setLocalStorageUserSetting(key, value); | ||
| }, | ||
| getBoolean: (key: string, def: boolean = false): boolean => { | ||
| return localUserSettings.getString(key, String(def)) === 'true'; | ||
| }, | ||
| setBoolean: (key: string, value: boolean) => { | ||
| localUserSettings.setString(key, String(value)); | ||
| }, | ||
| getJsonObject: <T extends Record<string, any>>(key: string, def: T): T => { | ||
| const value = getLocalStorageUserSetting(key); | ||
| try { | ||
| const decoded = value !== null ? JSON.parse(value) : def; | ||
| return decoded ?? def; | ||
| } catch (e) { | ||
| console.error(`Unable to parse JSON value for local user settings ${key}=${value}`, e); | ||
| } | ||
| return def; | ||
| }, | ||
| setJsonObject: <T extends Record<string, any>>(key: string, value: T) => { | ||
| localUserSettings.setString(key, JSON.stringify(value)); | ||
| }, | ||
| }; | ||
|
|
||
| window.localUserSettings = localUserSettings; | ||
|
wxiaoguang marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.