AdminCustomizeContainer.js 13 KB

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