Parcourir la source

Merge pull request #23026 from overleaf/mj-wc-settings-second-try

[web] Move write and cite setting storage

GitOrigin-RevId: 88a234c15b4dd2a9b451170e3b474d88bb6d45f7
Mathias Jakobsen il y a 1 an
Parent
commit
77dd468c20

+ 1 - 0
services/web/app/src/Features/Project/ProjectController.js

@@ -792,6 +792,7 @@ const _ProjectController = {
           lineHeight: user.ace.lineHeight || 'normal',
           overallTheme: user.ace.overallTheme,
           mathPreview: user.ace.mathPreview,
+          referencesSearchMode: user.ace.referencesSearchMode,
         },
         privilegeLevel,
         anonymous,

+ 5 - 0
services/web/app/src/Features/User/UserController.js

@@ -386,6 +386,11 @@ async function updateUserSettings(req, res, next) {
   if (req.body.mathPreview != null) {
     user.ace.mathPreview = req.body.mathPreview
   }
+  if (req.body.referencesSearchMode != null) {
+    const mode =
+      req.body.referencesSearchMode === 'simple' ? 'simple' : 'advanced'
+    user.ace.referencesSearchMode = mode
+  }
   await user.save()
 
   const newEmail = req.body.email?.trim().toLowerCase()

+ 1 - 0
services/web/app/src/models/User.js

@@ -97,6 +97,7 @@ const UserSchema = new Schema(
       fontFamily: { type: String },
       lineHeight: { type: String },
       mathPreview: { type: Boolean, default: true },
+      referencesSearchMode: { type: String, default: 'advanced' }, // 'advanced' or 'simple'
     },
     features: {
       collaborators: {

+ 1 - 0
services/web/frontend/js/features/source-editor/extensions/index.ts

@@ -115,6 +115,7 @@ export const createExtensions = (options: Record<string, any>): Extension[] => [
   autoComplete({
     enabled: options.settings.autoComplete,
     projectFeatures: options.projectFeatures,
+    referencesSearchMode: options.settings.referencesSearchMode,
   }),
 
   // NOTE: `keybindings` needs to be before `language` so that Vim/Emacs bindings take

+ 7 - 0
services/web/frontend/js/features/source-editor/hooks/use-codemirror-scope.ts

@@ -96,6 +96,7 @@ function useCodeMirrorScope(view: EditorView) {
     mode,
     syntaxValidation,
     mathPreview,
+    referencesSearchMode,
   } = userSettings
 
   const [cursorHighlights] = useScopeValue<Record<string, Highlight[]>>(
@@ -169,6 +170,7 @@ function useCodeMirrorScope(view: EditorView) {
     mode,
     syntaxValidation,
     mathPreview,
+    referencesSearchMode,
   })
 
   const currentDocRef = useRef({
@@ -430,6 +432,7 @@ function useCodeMirrorScope(view: EditorView) {
         setAutoComplete({
           enabled: autoComplete,
           projectFeatures: projectFeaturesRef.current,
+          referencesSearchMode: settingsRef.current.referencesSearchMode,
         })
       )
     })
@@ -458,6 +461,10 @@ function useCodeMirrorScope(view: EditorView) {
     })
   }, [view, mathPreview])
 
+  useEffect(() => {
+    settingsRef.current.referencesSearchMode = referencesSearchMode
+  }, [referencesSearchMode])
+
   const emitSyncToPdf = useScopeEventEmitter('cursor:editor:syncToPdf')
 
   const handleGoToLine = useCallback(

+ 1 - 0
services/web/frontend/js/shared/context/user-settings-context.tsx

@@ -25,6 +25,7 @@ const defaultSettings: UserSettings = {
   fontFamily: 'monaco',
   lineHeight: 'normal',
   mathPreview: true,
+  referencesSearchMode: 'advanced',
 }
 
 type UserSettingsContextValue = {

+ 27 - 0
services/web/test/unit/src/User/UserControllerTests.js

@@ -425,6 +425,33 @@ describe('UserController', function () {
       this.UserController.updateUserSettings(this.req, this.res)
     })
 
+    it('should set referencesSearchMode to advanced', function (done) {
+      this.req.body = { referencesSearchMode: 'advanced' }
+      this.res.sendStatus = code => {
+        this.user.ace.referencesSearchMode.should.equal('advanced')
+        done()
+      }
+      this.UserController.updateUserSettings(this.req, this.res)
+    })
+
+    it('should set referencesSearchMode to simple', function (done) {
+      this.req.body = { referencesSearchMode: 'simple' }
+      this.res.sendStatus = code => {
+        this.user.ace.referencesSearchMode.should.equal('simple')
+        done()
+      }
+      this.UserController.updateUserSettings(this.req, this.res)
+    })
+
+    it('should not allow arbitrary referencesSearchMode', function (done) {
+      this.req.body = { referencesSearchMode: 'foobar' }
+      this.res.sendStatus = code => {
+        this.user.ace.referencesSearchMode.should.equal('advanced')
+        done()
+      }
+      this.UserController.updateUserSettings(this.req, this.res)
+    })
+
     it('should send an error if the email is 0 len', function (done) {
       this.req.body.email = ''
       this.res.sendStatus = function (code) {

+ 1 - 0
services/web/types/user-settings.ts

@@ -15,4 +15,5 @@ export type UserSettings = {
   fontFamily: FontFamily
   lineHeight: LineHeight
   mathPreview: boolean
+  referencesSearchMode: 'advanced' | 'simple'
 }