api.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. type SaveProjectSettings = {
  50. projectId: string
  51. } & Partial<ProjectSettingsScope>
  52. export const saveProjectSettings = async ({
  53. projectId,
  54. ...data
  55. }: SaveProjectSettings) => {
  56. let reqData: ProjectSettingsRequestBody = {}
  57. if (data.rootDoc_id) {
  58. const val = data.rootDoc_id
  59. delete data.rootDoc_id
  60. reqData = {
  61. ...data,
  62. rootDocId: val,
  63. }
  64. } else {
  65. reqData = data
  66. }
  67. await postJSON<never>(`/project/${projectId}/settings`, {
  68. body: {
  69. _csrf: window.csrfToken,
  70. ...reqData,
  71. },
  72. })
  73. }