AdminCustomizeContainer.js 16 KB

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