AdminCustomizeContainer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. currentRecentCreatedLimit: 10,
  24. isEnabledStaleNotification: false,
  25. isAllReplyShown: false,
  26. currentHighlightJsStyleId: '',
  27. isHighlightJsStyleBorderEnabled: false,
  28. currentCustomizeTitle: '',
  29. currentCustomizeHeader: '',
  30. currentCustomizeCss: '',
  31. currentCustomizeScript: '',
  32. /* eslint-disable quote-props, no-multi-spaces */
  33. highlightJsCssSelectorOptions: {
  34. 'github': { name: '[Light] GitHub', border: false },
  35. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  36. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  37. 'xcode': { name: '[Light] Xcode', border: true },
  38. 'vs': { name: '[Light] Vs', border: true },
  39. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  40. 'hybrid': { name: '[Dark] Hybrid', border: false },
  41. 'monokai': { name: '[Dark] Monokai', border: false },
  42. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  43. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  44. },
  45. /* eslint-enable quote-props, no-multi-spaces */
  46. };
  47. }
  48. /**
  49. * Workaround for the mangling in production build to break constructor.name
  50. */
  51. static getClassName() {
  52. return 'AdminCustomizeContainer';
  53. }
  54. /**
  55. * retrieve customize data
  56. */
  57. async retrieveCustomizeData() {
  58. try {
  59. const response = await this.appContainer.apiv3.get('/customize-setting/');
  60. const { customizeParams } = response.data;
  61. this.setState({
  62. currentTheme: customizeParams.themeType,
  63. isEnabledTimeline: customizeParams.isEnabledTimeline,
  64. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  65. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  66. currentRecentCreatedLimit: customizeParams.recentCreatedLimit,
  67. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  68. isAllReplyShown: customizeParams.isAllReplyShown,
  69. currentHighlightJsStyleId: customizeParams.styleName,
  70. isHighlightJsStyleBorderEnabled: customizeParams.styleBorder,
  71. currentCustomizeTitle: customizeParams.customizeTitle,
  72. currentCustomizeHeader: customizeParams.customizeHeader,
  73. currentCustomizeCss: customizeParams.customizeCss,
  74. currentCustomizeScript: customizeParams.customizeScript,
  75. });
  76. // search style name from object for display
  77. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[customizeParams.styleName].name });
  78. }
  79. catch (err) {
  80. this.setState({ retrieveError: err });
  81. logger.error(err);
  82. throw new Error('Failed to fetch data');
  83. }
  84. }
  85. /**
  86. * Switch themeType
  87. */
  88. switchThemeType(themeName) {
  89. this.setState({ currentTheme: themeName });
  90. // preview if production
  91. if (process.env.NODE_ENV !== 'development') {
  92. this.previewTheme(themeName);
  93. }
  94. }
  95. /**
  96. * Switch enabledTimeLine
  97. */
  98. switchEnableTimeline() {
  99. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  100. }
  101. /**
  102. * Switch savedStatesOfTabChanges
  103. */
  104. switchSavedStatesOfTabChanges() {
  105. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  106. }
  107. /**
  108. * Switch enabledAttachTitleHeader
  109. */
  110. switchEnabledAttachTitleHeader() {
  111. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  112. }
  113. /**
  114. * Switch recentCreatedLimit
  115. */
  116. switchRecentCreatedLimit(value) {
  117. this.setState({ currentRecentCreatedLimit: value });
  118. }
  119. /**
  120. * Switch enabledStaleNotification
  121. */
  122. switchEnableStaleNotification() {
  123. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  124. }
  125. /**
  126. * Switch isAllReplyShown
  127. */
  128. switchIsAllReplyShown() {
  129. this.setState({ isAllReplyShown: !this.state.isAllReplyShown });
  130. }
  131. /**
  132. * Switch highlightJsStyle
  133. */
  134. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  135. this.setState({ currentHighlightJsStyleId: styleId });
  136. this.setState({ currentHighlightJsStyleName: styleName });
  137. // recommended settings are applied
  138. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  139. this.previewHighlightJsStyle(styleId);
  140. }
  141. /**
  142. * Switch highlightJsStyleBorder
  143. */
  144. switchHighlightJsStyleBorder() {
  145. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  146. }
  147. /**
  148. * Change customize Title
  149. */
  150. changeCustomizeTitle(inputValue) {
  151. this.setState({ currentCustomizeTitle: inputValue });
  152. }
  153. /**
  154. * Change customize Html header
  155. */
  156. changeCustomizeHeader(inputValue) {
  157. this.setState({ currentCustomizeHeader: inputValue });
  158. }
  159. /**
  160. * Change customize css
  161. */
  162. changeCustomizeCss(inputValue) {
  163. this.setState({ currentCustomizeCss: inputValue });
  164. }
  165. /**
  166. * Change customize script
  167. */
  168. changeCustomizeScript(inpuValue) {
  169. this.setState({ currentCustomizeScript: inpuValue });
  170. }
  171. /**
  172. * Preview theme
  173. * @param {string} themeName
  174. */
  175. async previewTheme(themeName) {
  176. try {
  177. // get theme asset path
  178. const response = await this.appContainer.apiv3.get('/customize-setting/theme/asset-path', { themeName });
  179. const { assetPath } = response.data;
  180. const themeLink = document.getElementById('grw-theme-link');
  181. themeLink.setAttribute('href', assetPath);
  182. }
  183. catch (err) {
  184. toastError(err);
  185. }
  186. }
  187. /**
  188. * Preview hljs style
  189. * @param {string} styleId
  190. */
  191. previewHighlightJsStyle(styleId) {
  192. const styleLInk = document.querySelectorAll('#grw-hljs-container-for-demo link')[0];
  193. // replace css url
  194. // see https://regex101.com/r/gBNZYu/4
  195. styleLInk.href = styleLInk.href.replace(/[^/]+\.css$/, `${styleId}.css`);
  196. }
  197. /**
  198. * Update theme
  199. * @memberOf AdminCustomizeContainer
  200. */
  201. async updateCustomizeTheme() {
  202. try {
  203. const response = await this.appContainer.apiv3.put('/customize-setting/theme', {
  204. themeType: this.state.currentTheme,
  205. });
  206. const { customizedParams } = response.data;
  207. this.setState({
  208. themeType: customizedParams.themeType,
  209. });
  210. }
  211. catch (err) {
  212. logger.error(err);
  213. throw new Error('Failed to update data');
  214. }
  215. }
  216. /**
  217. * Update function
  218. * @memberOf AdminCustomizeContainer
  219. */
  220. async updateCustomizeFunction() {
  221. try {
  222. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  223. isEnabledTimeline: this.state.isEnabledTimeline,
  224. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  225. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  226. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  227. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  228. isAllReplyShown: this.state.isAllReplyShown,
  229. });
  230. const { customizedParams } = response.data;
  231. this.setState({
  232. isEnabledTimeline: customizedParams.isEnabledTimeline,
  233. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  234. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  235. recentCreatedLimit: customizedParams.currentRecentCreatedLimit,
  236. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  237. isAllReplyShown: customizedParams.isAllReplyShown,
  238. });
  239. }
  240. catch (err) {
  241. logger.error(err);
  242. throw new Error('Failed to update data');
  243. }
  244. }
  245. /**
  246. * Update code highlight
  247. * @memberOf AdminCustomizeContainer
  248. */
  249. async updateHighlightJsStyle() {
  250. try {
  251. const response = await this.appContainer.apiv3.put('/customize-setting/highlight', {
  252. highlightJsStyle: this.state.currentHighlightJsStyleId,
  253. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  254. });
  255. const { customizedParams } = response.data;
  256. this.setState({
  257. highlightJsStyle: customizedParams.highlightJsStyle,
  258. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  259. });
  260. }
  261. catch (err) {
  262. logger.error(err);
  263. throw new Error('Failed to update data');
  264. }
  265. }
  266. /**
  267. * Update customTitle
  268. * @memberOf AdminCustomizeContainer
  269. */
  270. async updateCustomizeTitle() {
  271. try {
  272. const response = await this.appContainer.apiv3.put('/customize-setting/customize-title', {
  273. customizeTitle: this.state.currentCustomizeTitle,
  274. });
  275. const { customizedParams } = response.data;
  276. this.setState({
  277. customizeTitle: customizedParams.customizeTitle,
  278. });
  279. }
  280. catch (err) {
  281. logger.error(err);
  282. throw new Error('Failed to update data');
  283. }
  284. }
  285. /**
  286. * Update customHeader
  287. * @memberOf AdminCustomizeContainer
  288. */
  289. async updateCustomizeHeader() {
  290. try {
  291. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  292. customizeHeader: this.state.currentCustomizeHeader,
  293. });
  294. const { customizedParams } = response.data;
  295. this.setState({
  296. currentCustomizeHeader: customizedParams.customizeHeader,
  297. });
  298. }
  299. catch (err) {
  300. logger.error(err);
  301. throw new Error('Failed to update data');
  302. }
  303. }
  304. /**
  305. * Update customCss
  306. * @memberOf AdminCustomizeContainer
  307. */
  308. async updateCustomizeCss() {
  309. try {
  310. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  311. customizeCss: this.state.currentCustomizeCss,
  312. });
  313. const { customizedParams } = response.data;
  314. this.setState({
  315. currentCustomizeCss: customizedParams.customizeCss,
  316. });
  317. }
  318. catch (err) {
  319. logger.error(err);
  320. throw new Error('Failed to update data');
  321. }
  322. }
  323. /**
  324. * Update customize script
  325. * @memberOf AdminCustomizeContainer
  326. * @return {string} Customize scripts
  327. */
  328. async updateCustomizeScript() {
  329. try {
  330. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  331. customizeScript: this.state.currentCustomizeScript,
  332. });
  333. const { customizedParams } = response.data;
  334. this.setState({
  335. currentCustomizeScript: customizedParams.customizeScript,
  336. });
  337. }
  338. catch (err) {
  339. logger.error(err);
  340. throw new Error('Failed to update data');
  341. }
  342. }
  343. }