AdminCustomizeContainer.js 12 KB

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