api.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type {
  2. FontFamily,
  3. LineHeight,
  4. OverallTheme,
  5. } from '../../../../../modules/source-editor/frontend/js/extensions/theme'
  6. import type {
  7. Keybindings,
  8. PdfViewer,
  9. ProjectCompiler,
  10. } from '../../../../../types/project-settings'
  11. import { sendMB } from '../../../infrastructure/event-tracking'
  12. import { postJSON } from '../../../infrastructure/fetch-json'
  13. export type UserSettings = {
  14. pdfViewer: PdfViewer
  15. autoComplete: boolean
  16. autoPairDelimiters: boolean
  17. syntaxValidation: boolean
  18. editorTheme: string
  19. overallTheme: OverallTheme
  20. mode: Keybindings
  21. fontSize: string
  22. fontFamily: FontFamily
  23. lineHeight: LineHeight
  24. }
  25. export type ProjectSettings = {
  26. compiler: ProjectCompiler
  27. imageName: string
  28. rootDocId: string
  29. spellCheckLanguage: string
  30. }
  31. type SaveUserSettings = Partial<
  32. UserSettings & {
  33. spellCheckLanguage: ProjectSettings['spellCheckLanguage']
  34. }
  35. >
  36. export function saveUserSettings(
  37. key: keyof SaveUserSettings,
  38. value: SaveUserSettings[keyof SaveUserSettings]
  39. ) {
  40. sendMB('setting-changed', {
  41. changedSetting: key,
  42. changedSettingVal: value,
  43. })
  44. postJSON('/user/settings', {
  45. body: {
  46. [key]: value,
  47. },
  48. }).catch(console.error)
  49. }
  50. export const saveProjectSettings = async (
  51. projectId: string,
  52. data: Partial<ProjectSettings>
  53. ) => {
  54. await postJSON<never>(`/project/${projectId}/settings`, {
  55. body: {
  56. ...data,
  57. },
  58. })
  59. }