AdminCustomizeContainer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import { Container } from 'unstated';
  2. import loggerFactory from '~/utils/logger';
  3. import { toastError } from '../util/apiNotification';
  4. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  5. // eslint-disable-next-line no-unused-vars
  6. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  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. /* eslint-enable quote-props, no-multi-spaces */
  51. };
  52. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  53. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  54. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  55. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  56. }
  57. /**
  58. * Workaround for the mangling in production build to break constructor.name
  59. */
  60. static getClassName() {
  61. return 'AdminCustomizeContainer';
  62. }
  63. /**
  64. * retrieve customize data
  65. */
  66. async retrieveCustomizeData() {
  67. try {
  68. const response = await apiv3Get('/customize-setting/');
  69. const { customizeParams } = response.data;
  70. this.setState({
  71. currentTheme: customizeParams.themeType,
  72. isEnabledTimeline: customizeParams.isEnabledTimeline,
  73. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  74. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  75. pageLimitationS: customizeParams.pageLimitationS,
  76. pageLimitationM: customizeParams.pageLimitationM,
  77. pageLimitationL: customizeParams.pageLimitationL,
  78. pageLimitationXL: customizeParams.pageLimitationXL,
  79. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  80. isAllReplyShown: customizeParams.isAllReplyShown,
  81. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  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 isSearchScopeChildrenAsDefault
  164. */
  165. switchIsSearchScopeChildrenAsDefault() {
  166. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  167. }
  168. /**
  169. * Switch highlightJsStyle
  170. */
  171. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  172. this.setState({ currentHighlightJsStyleId: styleId });
  173. this.setState({ currentHighlightJsStyleName: styleName });
  174. // recommended settings are applied
  175. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  176. this.previewHighlightJsStyle(styleId);
  177. }
  178. /**
  179. * Switch highlightJsStyleBorder
  180. */
  181. switchHighlightJsStyleBorder() {
  182. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  183. }
  184. /**
  185. * Change customize Title
  186. */
  187. changeCustomizeTitle(inputValue) {
  188. this.setState({ currentCustomizeTitle: inputValue });
  189. }
  190. /**
  191. * Change customize Html header
  192. */
  193. changeCustomizeHeader(inputValue) {
  194. this.setState({ currentCustomizeHeader: inputValue });
  195. }
  196. /**
  197. * Change customize css
  198. */
  199. changeCustomizeCss(inputValue) {
  200. this.setState({ currentCustomizeCss: inputValue });
  201. }
  202. /**
  203. * Change customize script
  204. */
  205. changeCustomizeScript(inpuValue) {
  206. this.setState({ currentCustomizeScript: inpuValue });
  207. }
  208. /**
  209. * Preview theme
  210. * @param {string} themeName
  211. */
  212. async previewTheme(themeName) {
  213. try {
  214. // get theme asset path
  215. const response = await apiv3Get('/customize-setting/theme/asset-path', { themeName });
  216. const { assetPath } = response.data;
  217. const themeLink = document.getElementById('grw-theme-link');
  218. themeLink.setAttribute('href', assetPath);
  219. }
  220. catch (err) {
  221. toastError(err);
  222. }
  223. }
  224. /**
  225. * Preview hljs style
  226. * @param {string} styleId
  227. */
  228. previewHighlightJsStyle(styleId) {
  229. const styleLInk = document.querySelectorAll('#grw-hljs-container-for-demo link')[0];
  230. // replace css url
  231. // see https://regex101.com/r/gBNZYu/4
  232. styleLInk.href = styleLInk.href.replace(/[^/]+\.css$/, `${styleId}.css`);
  233. }
  234. /**
  235. * Update theme
  236. * @memberOf AdminCustomizeContainer
  237. */
  238. async updateCustomizeTheme() {
  239. try {
  240. const response = await apiv3Put('/customize-setting/theme', {
  241. themeType: this.state.currentTheme,
  242. });
  243. const { customizedParams } = response.data;
  244. this.setState({
  245. themeType: customizedParams.themeType,
  246. });
  247. }
  248. catch (err) {
  249. logger.error(err);
  250. throw new Error('Failed to update data');
  251. }
  252. }
  253. /**
  254. * Update function
  255. * @memberOf AdminCustomizeContainer
  256. */
  257. async updateCustomizeFunction() {
  258. try {
  259. const response = await apiv3Put('/customize-setting/function', {
  260. isEnabledTimeline: this.state.isEnabledTimeline,
  261. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  262. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  263. pageLimitationS: this.state.pageLimitationS,
  264. pageLimitationM: this.state.pageLimitationM,
  265. pageLimitationL: this.state.pageLimitationL,
  266. pageLimitationXL: this.state.pageLimitationXL,
  267. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  268. isAllReplyShown: this.state.isAllReplyShown,
  269. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  270. });
  271. const { customizedParams } = response.data;
  272. this.setState({
  273. isEnabledTimeline: customizedParams.isEnabledTimeline,
  274. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  275. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  276. pageLimitationS: customizedParams.pageLimitationS,
  277. pageLimitationM: customizedParams.pageLimitationM,
  278. pageLimitationL: customizedParams.pageLimitationL,
  279. pageLimitationXL: customizedParams.pageLimitationXL,
  280. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  281. isAllReplyShown: customizedParams.isAllReplyShown,
  282. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  283. });
  284. }
  285. catch (err) {
  286. logger.error(err);
  287. throw new Error('Failed to update data');
  288. }
  289. }
  290. /**
  291. * Update code highlight
  292. * @memberOf AdminCustomizeContainer
  293. */
  294. async updateHighlightJsStyle() {
  295. try {
  296. const response = await apiv3Put('/customize-setting/highlight', {
  297. highlightJsStyle: this.state.currentHighlightJsStyleId,
  298. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  299. });
  300. const { customizedParams } = response.data;
  301. this.setState({
  302. highlightJsStyle: customizedParams.highlightJsStyle,
  303. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  304. });
  305. }
  306. catch (err) {
  307. logger.error(err);
  308. throw new Error('Failed to update data');
  309. }
  310. }
  311. /**
  312. * Update customTitle
  313. * @memberOf AdminCustomizeContainer
  314. */
  315. async updateCustomizeTitle() {
  316. try {
  317. const response = await apiv3Put('/customize-setting/customize-title', {
  318. customizeTitle: this.state.currentCustomizeTitle,
  319. });
  320. const { customizedParams } = response.data;
  321. this.setState({
  322. customizeTitle: customizedParams.customizeTitle,
  323. });
  324. }
  325. catch (err) {
  326. logger.error(err);
  327. throw new Error('Failed to update data');
  328. }
  329. }
  330. /**
  331. * Update customHeader
  332. * @memberOf AdminCustomizeContainer
  333. */
  334. async updateCustomizeHeader() {
  335. try {
  336. const response = await apiv3Put('/customize-setting/customize-header', {
  337. customizeHeader: this.state.currentCustomizeHeader,
  338. });
  339. const { customizedParams } = response.data;
  340. this.setState({
  341. currentCustomizeHeader: customizedParams.customizeHeader,
  342. });
  343. }
  344. catch (err) {
  345. logger.error(err);
  346. throw new Error('Failed to update data');
  347. }
  348. }
  349. /**
  350. * Update customCss
  351. * @memberOf AdminCustomizeContainer
  352. */
  353. async updateCustomizeCss() {
  354. try {
  355. const response = await apiv3Put('/customize-setting/customize-css', {
  356. customizeCss: this.state.currentCustomizeCss,
  357. });
  358. const { customizedParams } = response.data;
  359. this.setState({
  360. currentCustomizeCss: customizedParams.customizeCss,
  361. });
  362. }
  363. catch (err) {
  364. logger.error(err);
  365. throw new Error('Failed to update data');
  366. }
  367. }
  368. /**
  369. * Update customize script
  370. * @memberOf AdminCustomizeContainer
  371. * @return {string} Customize scripts
  372. */
  373. async updateCustomizeScript() {
  374. try {
  375. const response = await apiv3Put('/customize-setting/customize-script', {
  376. customizeScript: this.state.currentCustomizeScript,
  377. });
  378. const { customizedParams } = response.data;
  379. this.setState({
  380. currentCustomizeScript: customizedParams.customizeScript,
  381. });
  382. }
  383. catch (err) {
  384. logger.error(err);
  385. throw new Error('Failed to update data');
  386. }
  387. }
  388. }