AdminCustomizeContainer.js 12 KB

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