AdminCustomizeContainer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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() {
  13. super();
  14. this.state = {
  15. retrieveError: null,
  16. // set dummy value tile for using suspense
  17. currentTheme: 'default',
  18. isEnabledTimeline: false,
  19. isSavedStatesOfTabChanges: false,
  20. isEnabledAttachTitleHeader: false,
  21. pageLimitationS: null,
  22. pageLimitationM: null,
  23. pageLimitationL: null,
  24. pageLimitationXL: null,
  25. isEnabledStaleNotification: false,
  26. isAllReplyShown: false,
  27. isSearchScopeChildrenAsDefault: false,
  28. currentHighlightJsStyleId: '',
  29. isHighlightJsStyleBorderEnabled: false,
  30. currentCustomizeTitle: '',
  31. currentCustomizeHeader: '',
  32. currentCustomizeCss: '',
  33. currentCustomizeScript: '',
  34. /* eslint-disable quote-props, no-multi-spaces */
  35. highlightJsCssSelectorOptions: {
  36. 'github': { name: '[Light] GitHub', border: false },
  37. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  38. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  39. 'xcode': { name: '[Light] Xcode', border: true },
  40. 'vs': { name: '[Light] Vs', border: true },
  41. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  42. 'hybrid': { name: '[Dark] Hybrid', border: false },
  43. 'monokai': { name: '[Dark] Monokai', border: false },
  44. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  45. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  46. },
  47. /* eslint-enable quote-props, no-multi-spaces */
  48. };
  49. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  50. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  51. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  52. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  53. }
  54. /**
  55. * Workaround for the mangling in production build to break constructor.name
  56. */
  57. static getClassName() {
  58. return 'AdminCustomizeContainer';
  59. }
  60. /**
  61. * retrieve customize data
  62. */
  63. async retrieveCustomizeData() {
  64. try {
  65. const response = await apiv3Get('/customize-setting/');
  66. const { customizeParams } = response.data;
  67. this.setState({
  68. currentTheme: customizeParams.themeType,
  69. isEnabledTimeline: customizeParams.isEnabledTimeline,
  70. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  71. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  72. pageLimitationS: customizeParams.pageLimitationS,
  73. pageLimitationM: customizeParams.pageLimitationM,
  74. pageLimitationL: customizeParams.pageLimitationL,
  75. pageLimitationXL: customizeParams.pageLimitationXL,
  76. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  77. isAllReplyShown: customizeParams.isAllReplyShown,
  78. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  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 isSearchScopeChildrenAsDefault
  161. */
  162. switchIsSearchScopeChildrenAsDefault() {
  163. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  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 apiv3Get('/customize-setting/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 theme
  233. * @memberOf AdminCustomizeContainer
  234. */
  235. async updateCustomizeTheme() {
  236. try {
  237. const response = await apiv3Put('/customize-setting/theme', {
  238. themeType: this.state.currentTheme,
  239. });
  240. const { customizedParams } = response.data;
  241. this.setState({
  242. themeType: customizedParams.themeType,
  243. });
  244. }
  245. catch (err) {
  246. logger.error(err);
  247. throw new Error('Failed to update data');
  248. }
  249. }
  250. /**
  251. * Update function
  252. * @memberOf AdminCustomizeContainer
  253. */
  254. async updateCustomizeFunction() {
  255. try {
  256. const response = await apiv3Put('/customize-setting/function', {
  257. isEnabledTimeline: this.state.isEnabledTimeline,
  258. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  259. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  260. pageLimitationS: this.state.pageLimitationS,
  261. pageLimitationM: this.state.pageLimitationM,
  262. pageLimitationL: this.state.pageLimitationL,
  263. pageLimitationXL: this.state.pageLimitationXL,
  264. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  265. isAllReplyShown: this.state.isAllReplyShown,
  266. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  267. });
  268. const { customizedParams } = response.data;
  269. this.setState({
  270. isEnabledTimeline: customizedParams.isEnabledTimeline,
  271. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  272. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  273. pageLimitationS: customizedParams.pageLimitationS,
  274. pageLimitationM: customizedParams.pageLimitationM,
  275. pageLimitationL: customizedParams.pageLimitationL,
  276. pageLimitationXL: customizedParams.pageLimitationXL,
  277. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  278. isAllReplyShown: customizedParams.isAllReplyShown,
  279. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  280. });
  281. }
  282. catch (err) {
  283. logger.error(err);
  284. throw new Error('Failed to update data');
  285. }
  286. }
  287. /**
  288. * Update code highlight
  289. * @memberOf AdminCustomizeContainer
  290. */
  291. async updateHighlightJsStyle() {
  292. try {
  293. const response = await apiv3Put('/customize-setting/highlight', {
  294. highlightJsStyle: this.state.currentHighlightJsStyleId,
  295. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  296. });
  297. const { customizedParams } = response.data;
  298. this.setState({
  299. highlightJsStyle: customizedParams.highlightJsStyle,
  300. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  301. });
  302. }
  303. catch (err) {
  304. logger.error(err);
  305. throw new Error('Failed to update data');
  306. }
  307. }
  308. /**
  309. * Update customTitle
  310. * @memberOf AdminCustomizeContainer
  311. */
  312. async updateCustomizeTitle() {
  313. try {
  314. const response = await apiv3Put('/customize-setting/customize-title', {
  315. customizeTitle: this.state.currentCustomizeTitle,
  316. });
  317. const { customizedParams } = response.data;
  318. this.setState({
  319. customizeTitle: customizedParams.customizeTitle,
  320. });
  321. }
  322. catch (err) {
  323. logger.error(err);
  324. throw new Error('Failed to update data');
  325. }
  326. }
  327. /**
  328. * Update customHeader
  329. * @memberOf AdminCustomizeContainer
  330. */
  331. async updateCustomizeHeader() {
  332. try {
  333. const response = await apiv3Put('/customize-setting/customize-header', {
  334. customizeHeader: this.state.currentCustomizeHeader,
  335. });
  336. const { customizedParams } = response.data;
  337. this.setState({
  338. currentCustomizeHeader: customizedParams.customizeHeader,
  339. });
  340. }
  341. catch (err) {
  342. logger.error(err);
  343. throw new Error('Failed to update data');
  344. }
  345. }
  346. /**
  347. * Update customCss
  348. * @memberOf AdminCustomizeContainer
  349. */
  350. async updateCustomizeCss() {
  351. try {
  352. const response = await apiv3Put('/customize-setting/customize-css', {
  353. customizeCss: this.state.currentCustomizeCss,
  354. });
  355. const { customizedParams } = response.data;
  356. this.setState({
  357. currentCustomizeCss: customizedParams.customizeCss,
  358. });
  359. }
  360. catch (err) {
  361. logger.error(err);
  362. throw new Error('Failed to update data');
  363. }
  364. }
  365. /**
  366. * Update customize script
  367. * @memberOf AdminCustomizeContainer
  368. * @return {string} Customize scripts
  369. */
  370. async updateCustomizeScript() {
  371. try {
  372. const response = await apiv3Put('/customize-setting/customize-script', {
  373. customizeScript: this.state.currentCustomizeScript,
  374. });
  375. const { customizedParams } = response.data;
  376. this.setState({
  377. currentCustomizeScript: customizedParams.customizeScript,
  378. });
  379. }
  380. catch (err) {
  381. logger.error(err);
  382. throw new Error('Failed to update data');
  383. }
  384. }
  385. }