AdminCustomizeContainer.js 12 KB

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