api.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. FontFamily,
  3. LineHeight,
  4. OverallTheme,
  5. } from '../../../../../modules/source-editor/frontend/js/extensions/theme'
  6. import {
  7. Keybindings,
  8. PdfViewer,
  9. ProjectCompiler,
  10. } from '../../../../../types/project-settings'
  11. import { postJSON } from '../../../infrastructure/fetch-json'
  12. export type UserSettingsScope = {
  13. pdfViewer: PdfViewer
  14. autoComplete: boolean
  15. autoPairDelimiters: boolean
  16. syntaxValidation: boolean
  17. editorTheme: string
  18. overallTheme: OverallTheme
  19. mode: Keybindings
  20. fontSize: string
  21. fontFamily: FontFamily
  22. lineHeight: LineHeight
  23. }
  24. export type ProjectSettingsScope = {
  25. compiler: ProjectCompiler
  26. imageName: string
  27. rootDoc_id: string
  28. spellCheckLanguage: string
  29. }
  30. type SaveUserSettings = Partial<
  31. UserSettingsScope & {
  32. spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage']
  33. }
  34. >
  35. export function saveUserSettings(data: SaveUserSettings) {
  36. postJSON('/user/settings', {
  37. body: {
  38. _csrf: window.csrfToken,
  39. ...data,
  40. },
  41. })
  42. }
  43. // server asks for "rootDocId" but client has "rootDoc_id"
  44. type ProjectSettingsRequestBody = Partial<
  45. Omit<ProjectSettingsScope, 'rootDoc_id'> & {
  46. rootDocId: string
  47. }
  48. >
  49. export const saveProjectSettings = async (
  50. projectId: string,
  51. data: Partial<ProjectSettingsScope>
  52. ) => {
  53. let reqData: ProjectSettingsRequestBody = {}
  54. if (data.rootDoc_id) {
  55. const val = data.rootDoc_id
  56. delete data.rootDoc_id
  57. reqData = {
  58. ...data,
  59. rootDocId: val,
  60. }
  61. } else {
  62. reqData = data
  63. }
  64. await postJSON<never>(`/project/${projectId}/settings`, {
  65. body: {
  66. _csrf: window.csrfToken,
  67. ...reqData,
  68. },
  69. })
  70. }