AdminCustomizeContainer.js 13 KB

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