api.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 function saveUserSettings(data: Partial<UserSettingsScope>) {
  25. postJSON('/user/settings', {
  26. body: {
  27. _csrf: window.csrfToken,
  28. ...data,
  29. },
  30. })
  31. }
  32. export type ProjectSettingsScope = {
  33. compiler: ProjectCompiler
  34. imageName: string
  35. rootDoc_id: string
  36. spellCheckLanguage: string
  37. }
  38. // server asks for "rootDocId" but client has "rootDoc_id"
  39. type ProjectSettingsRequestBody = Partial<
  40. Omit<ProjectSettingsScope, 'rootDoc_id'> & {
  41. rootDocId: string
  42. }
  43. >
  44. export const saveProjectSettings = async (
  45. projectId: string,
  46. data: Partial<ProjectSettingsScope>
  47. ) => {
  48. let reqData: ProjectSettingsRequestBody = {}
  49. if (data.rootDoc_id) {
  50. const val = data.rootDoc_id
  51. delete data.rootDoc_id
  52. reqData = {
  53. ...data,
  54. rootDocId: val,
  55. }
  56. } else {
  57. reqData = data
  58. }
  59. await postJSON<never>(`/project/${projectId}/settings`, {
  60. body: {
  61. _csrf: window.csrfToken,
  62. ...reqData,
  63. },
  64. })
  65. }