AdminCustomizeContainer.js 12 KB

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