AdminCustomizeContainer.js 12 KB

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