AdminCustomizeContainer.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import { Container } from 'unstated';
  2. import { AttachmentType } from '~/server/interfaces/attachment';
  3. import loggerFactory from '~/utils/logger';
  4. import { toastError } from '../util/apiNotification';
  5. import { apiPost } from '../util/apiv1-client';
  6. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  7. // eslint-disable-next-line no-unused-vars
  8. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  9. const DEFAULT_LOGO = '/images/logo.svg';
  10. /**
  11. * Service container for admin customize setting page (Customize.jsx)
  12. * @extends {Container} unstated Container
  13. */
  14. export default class AdminCustomizeContainer extends Container {
  15. constructor(appContainer) {
  16. super();
  17. this.appContainer = appContainer;
  18. this.dummyCurrentTheme = 0;
  19. this.dummyCurrentThemeForError = 1;
  20. this.state = {
  21. retrieveError: null,
  22. // set dummy value tile for using suspense
  23. currentTheme: this.dummyCurrentTheme,
  24. isEnabledTimeline: false,
  25. isSavedStatesOfTabChanges: false,
  26. isEnabledAttachTitleHeader: false,
  27. pageLimitationS: null,
  28. pageLimitationM: null,
  29. pageLimitationL: null,
  30. pageLimitationXL: null,
  31. isEnabledStaleNotification: false,
  32. isAllReplyShown: false,
  33. isSearchScopeChildrenAsDefault: false,
  34. currentHighlightJsStyleId: '',
  35. isHighlightJsStyleBorderEnabled: false,
  36. currentCustomizeTitle: '',
  37. currentCustomizeHeader: '',
  38. currentCustomizeCss: '',
  39. currentCustomizeScript: '',
  40. /* eslint-disable quote-props, no-multi-spaces */
  41. highlightJsCssSelectorOptions: {
  42. 'github': { name: '[Light] GitHub', border: false },
  43. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  44. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  45. 'xcode': { name: '[Light] Xcode', border: true },
  46. 'vs': { name: '[Light] Vs', border: true },
  47. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  48. 'hybrid': { name: '[Dark] Hybrid', border: false },
  49. 'monokai': { name: '[Dark] Monokai', border: false },
  50. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  51. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  52. },
  53. uploadedLogoSrc: null,
  54. defaultLogoSrc: DEFAULT_LOGO,
  55. isDefaultLogo: true,
  56. brandLogoAttachmentId: '',
  57. /* eslint-enable quote-props, no-multi-spaces */
  58. };
  59. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  60. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  61. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  62. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  63. this.deleteLogo = this.deleteLogo.bind(this);
  64. this.uploadBrandLogo = this.uploadBrandLogo.bind(this);
  65. }
  66. /**
  67. * Workaround for the mangling in production build to break constructor.name
  68. */
  69. static getClassName() {
  70. return 'AdminCustomizeContainer';
  71. }
  72. /**
  73. * retrieve customize data
  74. */
  75. async retrieveCustomizeData() {
  76. try {
  77. const response = await apiv3Get('/customize-setting/');
  78. const { customizeParams } = response.data;
  79. this.setState({
  80. currentTheme: customizeParams.themeType,
  81. isEnabledTimeline: customizeParams.isEnabledTimeline,
  82. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  83. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  84. pageLimitationS: customizeParams.pageLimitationS,
  85. pageLimitationM: customizeParams.pageLimitationM,
  86. pageLimitationL: customizeParams.pageLimitationL,
  87. pageLimitationXL: customizeParams.pageLimitationXL,
  88. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  89. isAllReplyShown: customizeParams.isAllReplyShown,
  90. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  91. currentHighlightJsStyleId: customizeParams.styleName,
  92. isHighlightJsStyleBorderEnabled: customizeParams.styleBorder,
  93. currentCustomizeTitle: customizeParams.customizeTitle,
  94. currentCustomizeHeader: customizeParams.customizeHeader,
  95. currentCustomizeCss: customizeParams.customizeCss,
  96. currentCustomizeScript: customizeParams.customizeScript,
  97. brandLogoAttachmentId: customizeParams.brandLogoAttachmentId,
  98. isDefaultLogo: customizeParams.uploadedLogoSrc == null ? true : customizeParams.isDefaultLogo,
  99. uploadedLogoSrc: customizeParams.uploadedLogoSrc,
  100. });
  101. // search style name from object for display
  102. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[customizeParams.styleName].name });
  103. }
  104. catch (err) {
  105. this.setState({ retrieveError: err });
  106. logger.error(err);
  107. throw new Error('Failed to fetch data');
  108. }
  109. }
  110. /**
  111. * Switch themeType
  112. */
  113. switchThemeType(themeName) {
  114. this.setState({ currentTheme: themeName });
  115. // preview if production
  116. if (process.env.NODE_ENV !== 'development') {
  117. this.previewTheme(themeName);
  118. }
  119. }
  120. /**
  121. * Switch enabledTimeLine
  122. */
  123. switchEnableTimeline() {
  124. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  125. }
  126. /**
  127. * Switch savedStatesOfTabChanges
  128. */
  129. switchSavedStatesOfTabChanges() {
  130. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  131. }
  132. /**
  133. * Switch enabledAttachTitleHeader
  134. */
  135. switchEnabledAttachTitleHeader() {
  136. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  137. }
  138. /**
  139. * S: Switch pageListLimitationS
  140. */
  141. switchPageListLimitationS(value) {
  142. this.setState({ pageLimitationS: value });
  143. }
  144. /**
  145. * M: Switch pageListLimitationM
  146. */
  147. switchPageListLimitationM(value) {
  148. this.setState({ pageLimitationM: value });
  149. }
  150. /**
  151. * L: Switch pageListLimitationL
  152. */
  153. switchPageListLimitationL(value) {
  154. this.setState({ pageLimitationL: value });
  155. }
  156. /**
  157. * XL: Switch pageListLimitationXL
  158. */
  159. switchPageListLimitationXL(value) {
  160. this.setState({ pageLimitationXL: value });
  161. }
  162. /**
  163. * Switch enabledStaleNotification
  164. */
  165. switchEnableStaleNotification() {
  166. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  167. }
  168. /**
  169. * Switch isAllReplyShown
  170. */
  171. switchIsAllReplyShown() {
  172. this.setState({ isAllReplyShown: !this.state.isAllReplyShown });
  173. }
  174. /**
  175. * Switch isSearchScopeChildrenAsDefault
  176. */
  177. switchIsSearchScopeChildrenAsDefault() {
  178. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  179. }
  180. /**
  181. * Switch highlightJsStyle
  182. */
  183. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  184. this.setState({ currentHighlightJsStyleId: styleId });
  185. this.setState({ currentHighlightJsStyleName: styleName });
  186. // recommended settings are applied
  187. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  188. this.previewHighlightJsStyle(styleId);
  189. }
  190. /**
  191. * Switch highlightJsStyleBorder
  192. */
  193. switchHighlightJsStyleBorder() {
  194. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  195. }
  196. /**
  197. * Change customize Title
  198. */
  199. changeCustomizeTitle(inputValue) {
  200. this.setState({ currentCustomizeTitle: inputValue });
  201. }
  202. /**
  203. * Change customize Html header
  204. */
  205. changeCustomizeHeader(inputValue) {
  206. this.setState({ currentCustomizeHeader: inputValue });
  207. }
  208. /**
  209. * Change customize css
  210. */
  211. changeCustomizeCss(inputValue) {
  212. this.setState({ currentCustomizeCss: inputValue });
  213. }
  214. /**
  215. * Change customize script
  216. */
  217. changeCustomizeScript(inpuValue) {
  218. this.setState({ currentCustomizeScript: inpuValue });
  219. }
  220. /**
  221. * Preview theme
  222. * @param {string} themeName
  223. */
  224. async previewTheme(themeName) {
  225. try {
  226. // get theme asset path
  227. const response = await apiv3Get('/customize-setting/theme/asset-path', { themeName });
  228. const { assetPath } = response.data;
  229. const themeLink = document.getElementById('grw-theme-link');
  230. themeLink.setAttribute('href', assetPath);
  231. }
  232. catch (err) {
  233. toastError(err);
  234. }
  235. }
  236. /**
  237. * Preview hljs style
  238. * @param {string} styleId
  239. */
  240. previewHighlightJsStyle(styleId) {
  241. const styleLInk = document.querySelectorAll('#grw-hljs-container-for-demo link')[0];
  242. // replace css url
  243. // see https://regex101.com/r/gBNZYu/4
  244. styleLInk.href = styleLInk.href.replace(/[^/]+\.css$/, `${styleId}.css`);
  245. }
  246. /**
  247. * Update theme
  248. * @memberOf AdminCustomizeContainer
  249. */
  250. async updateCustomizeTheme() {
  251. try {
  252. const response = await apiv3Put('/customize-setting/theme', {
  253. themeType: this.state.currentTheme,
  254. });
  255. const { customizedParams } = response.data;
  256. this.setState({
  257. themeType: customizedParams.themeType,
  258. });
  259. }
  260. catch (err) {
  261. logger.error(err);
  262. throw new Error('Failed to update data');
  263. }
  264. }
  265. /**
  266. * Update function
  267. * @memberOf AdminCustomizeContainer
  268. */
  269. async updateCustomizeFunction() {
  270. try {
  271. const response = await apiv3Put('/customize-setting/function', {
  272. isEnabledTimeline: this.state.isEnabledTimeline,
  273. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  274. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  275. pageLimitationS: this.state.pageLimitationS,
  276. pageLimitationM: this.state.pageLimitationM,
  277. pageLimitationL: this.state.pageLimitationL,
  278. pageLimitationXL: this.state.pageLimitationXL,
  279. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  280. isAllReplyShown: this.state.isAllReplyShown,
  281. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  282. });
  283. const { customizedParams } = response.data;
  284. this.setState({
  285. isEnabledTimeline: customizedParams.isEnabledTimeline,
  286. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  287. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  288. pageLimitationS: customizedParams.pageLimitationS,
  289. pageLimitationM: customizedParams.pageLimitationM,
  290. pageLimitationL: customizedParams.pageLimitationL,
  291. pageLimitationXL: customizedParams.pageLimitationXL,
  292. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  293. isAllReplyShown: customizedParams.isAllReplyShown,
  294. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  295. });
  296. }
  297. catch (err) {
  298. logger.error(err);
  299. throw new Error('Failed to update data');
  300. }
  301. }
  302. /**
  303. * Update code highlight
  304. * @memberOf AdminCustomizeContainer
  305. */
  306. async updateHighlightJsStyle() {
  307. try {
  308. const response = await apiv3Put('/customize-setting/highlight', {
  309. highlightJsStyle: this.state.currentHighlightJsStyleId,
  310. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  311. });
  312. const { customizedParams } = response.data;
  313. this.setState({
  314. highlightJsStyle: customizedParams.highlightJsStyle,
  315. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  316. });
  317. }
  318. catch (err) {
  319. logger.error(err);
  320. throw new Error('Failed to update data');
  321. }
  322. }
  323. /**
  324. * Update customTitle
  325. * @memberOf AdminCustomizeContainer
  326. */
  327. async updateCustomizeTitle() {
  328. try {
  329. const response = await apiv3Put('/customize-setting/customize-title', {
  330. customizeTitle: this.state.currentCustomizeTitle,
  331. });
  332. const { customizedParams } = response.data;
  333. this.setState({
  334. customizeTitle: customizedParams.customizeTitle,
  335. });
  336. }
  337. catch (err) {
  338. logger.error(err);
  339. throw new Error('Failed to update data');
  340. }
  341. }
  342. /**
  343. * Update customHeader
  344. * @memberOf AdminCustomizeContainer
  345. */
  346. async updateCustomizeHeader() {
  347. try {
  348. const response = await apiv3Put('/customize-setting/customize-header', {
  349. customizeHeader: this.state.currentCustomizeHeader,
  350. });
  351. const { customizedParams } = response.data;
  352. this.setState({
  353. currentCustomizeHeader: customizedParams.customizeHeader,
  354. });
  355. }
  356. catch (err) {
  357. logger.error(err);
  358. throw new Error('Failed to update data');
  359. }
  360. }
  361. /**
  362. * Update customCss
  363. * @memberOf AdminCustomizeContainer
  364. */
  365. async updateCustomizeCss() {
  366. try {
  367. const response = await apiv3Put('/customize-setting/customize-css', {
  368. customizeCss: this.state.currentCustomizeCss,
  369. });
  370. const { customizedParams } = response.data;
  371. this.setState({
  372. currentCustomizeCss: customizedParams.customizeCss,
  373. });
  374. }
  375. catch (err) {
  376. logger.error(err);
  377. throw new Error('Failed to update data');
  378. }
  379. }
  380. /**
  381. * Update customize script
  382. * @memberOf AdminCustomizeContainer
  383. * @return {string} Customize scripts
  384. */
  385. async updateCustomizeScript() {
  386. try {
  387. const response = await apiv3Put('/customize-setting/customize-script', {
  388. customizeScript: this.state.currentCustomizeScript,
  389. });
  390. const { customizedParams } = response.data;
  391. this.setState({
  392. currentCustomizeScript: customizedParams.customizeScript,
  393. });
  394. }
  395. catch (err) {
  396. logger.error(err);
  397. throw new Error('Failed to update data');
  398. }
  399. }
  400. async deleteLogo() {
  401. try {
  402. const formData = {
  403. _csrf: this.appContainer.csrfToken,
  404. brandLogoAttachmentId: this.state.brandLogoAttachmentId,
  405. };
  406. await apiPost('/attachments.removeBrandLogo', formData);
  407. this.setState({
  408. uploadedLogoSrc: null,
  409. brandLogoAttachmentId: null,
  410. });
  411. }
  412. catch (err) {
  413. this.setState({ retrieveError: err });
  414. logger.error(err);
  415. throw new Error('Failed to delete logo');
  416. }
  417. }
  418. async uploadBrandLogo(file) {
  419. try {
  420. const formData = new FormData();
  421. formData.append('file', file);
  422. formData.append('_csrf', this.appContainer.csrfToken);
  423. formData.append('attachmentType', AttachmentType.BRAND_LOGO);
  424. formData.append('brandLogoAttachmentId', this.state.brandLogoAttachmentId);
  425. const response = await apiPost('/attachments.uploadBrandLogo', formData);
  426. this.setState({
  427. uploadedLogoSrc: response.attachment.filePathProxied,
  428. brandLogoAttachmentId: response.attachment.id,
  429. });
  430. }
  431. catch (err) {
  432. this.setState({ retrieveError: err });
  433. logger.error(err);
  434. throw new Error('Failed to upload brand logo');
  435. }
  436. }
  437. switchDefaultLogo() {
  438. this.setState({ isDefaultLogo: !this.state.isDefaultLogo });
  439. }
  440. async updateCustomizeLogo() {
  441. try {
  442. const response = await apiv3Put('/customize-setting/customize-logo', {
  443. isDefaultLogo: this.state.isDefaultLogo,
  444. brandLogoAttachmentId: this.state.brandLogoAttachmentId,
  445. uploadedLogoSrc: this.state.uploadedLogoSrc,
  446. });
  447. const { customizedParams } = response.data;
  448. this.setState({
  449. isDefaultLogo: customizedParams.isDefaultLogo,
  450. brandLogoAttachmentId: customizedParams.brandLogoAttachmentId,
  451. uploadedLogoSrc: customizedParams.uploadedLogoSrc,
  452. });
  453. }
  454. catch (err) {
  455. logger.error(err);
  456. throw new Error('Failed to update data');
  457. }
  458. }
  459. }