AdminCustomizeContainer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. currentBehavior: '',
  19. isEnabledTimeline: false,
  20. isSavedStatesOfTabChanges: false,
  21. isEnabledAttachTitleHeader: false,
  22. currentRecentCreatedLimit: 10,
  23. isEnabledStaleNotification: 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. currentBehavior: customizeParams.behaviorType,
  63. isEnabledTimeline: customizeParams.isEnabledTimeline,
  64. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  65. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  66. currentRecentCreatedLimit: customizeParams.recentCreatedLimit,
  67. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  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. logger.error(err);
  80. toastError(new Error('Failed to fetch data'));
  81. }
  82. }
  83. /**
  84. * Switch layoutType
  85. */
  86. switchLayoutType(lauoutName) {
  87. this.setState({ currentLayout: lauoutName });
  88. }
  89. /**
  90. * Switch themeType
  91. */
  92. switchThemeType(themeName) {
  93. // can't choose theme when kibela
  94. if (this.state.currentLayout === 'kibela') {
  95. return;
  96. }
  97. this.setState({ currentTheme: themeName });
  98. // preview if production
  99. if (process.env.NODE_ENV !== 'development') {
  100. this.previewTheme(themeName);
  101. }
  102. }
  103. /**
  104. * Switch behaviorType
  105. */
  106. switchBehaviorType(behaviorName) {
  107. this.setState({ currentBehavior: behaviorName });
  108. }
  109. /**
  110. * Switch enabledTimeLine
  111. */
  112. switchEnableTimeline() {
  113. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  114. }
  115. /**
  116. * Switch savedStatesOfTabChanges
  117. */
  118. switchSavedStatesOfTabChanges() {
  119. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  120. }
  121. /**
  122. * Switch enabledAttachTitleHeader
  123. */
  124. switchEnabledAttachTitleHeader() {
  125. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  126. }
  127. /**
  128. * Switch recentCreatedLimit
  129. */
  130. switchRecentCreatedLimit(value) {
  131. this.setState({ currentRecentCreatedLimit: value });
  132. }
  133. /**
  134. * Switch enabledStaleNotification
  135. */
  136. switchEnableStaleNotification() {
  137. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  138. }
  139. /**
  140. * Switch highlightJsStyle
  141. */
  142. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  143. this.setState({ currentHighlightJsStyleId: styleId });
  144. this.setState({ currentHighlightJsStyleName: styleName });
  145. // recommended settings are applied
  146. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  147. this.previewHighlightJsStyle(styleId);
  148. }
  149. /**
  150. * Switch highlightJsStyleBorder
  151. */
  152. switchHighlightJsStyleBorder() {
  153. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  154. }
  155. /**
  156. * Change customize Title
  157. */
  158. changeCustomizeTitle(inputValue) {
  159. this.setState({ currentCustomizeTitle: inputValue });
  160. }
  161. /**
  162. * Change customize Html header
  163. */
  164. changeCustomizeHeader(inputValue) {
  165. this.setState({ currentCustomizeHeader: inputValue });
  166. }
  167. /**
  168. * Change customize css
  169. */
  170. changeCustomizeCss(inputValue) {
  171. this.setState({ currentCustomizeCss: inputValue });
  172. }
  173. /**
  174. * Change customize script
  175. */
  176. changeCustomizeScript(inpuValue) {
  177. this.setState({ currentCustomizeScript: inpuValue });
  178. }
  179. /**
  180. * Preview theme
  181. * @param {string} themeName
  182. */
  183. async previewTheme(themeName) {
  184. try {
  185. // get theme asset path
  186. const response = await this.appContainer.apiv3.get('/customize-setting/layout-theme/asset-path', { themeName });
  187. const { assetPath } = response.data;
  188. const themeLink = document.getElementById('grw-theme-link');
  189. themeLink.setAttribute('href', assetPath);
  190. }
  191. catch (err) {
  192. toastError(err);
  193. }
  194. }
  195. /**
  196. * Preview hljs style
  197. * @param {string} styleId
  198. */
  199. previewHighlightJsStyle(styleId) {
  200. const styleLInk = document.querySelectorAll('#grw-hljs-container-for-demo link')[0];
  201. // replace css url
  202. // see https://regex101.com/r/gBNZYu/4
  203. styleLInk.href = styleLInk.href.replace(/[^/]+\.css$/, `${styleId}.css`);
  204. }
  205. /**
  206. * Update layout
  207. * @memberOf AdminCustomizeContainer
  208. * @return {Array} Appearance
  209. */
  210. async updateCustomizeLayoutAndTheme() {
  211. const response = await this.appContainer.apiv3.put('/customize-setting/layout-theme', {
  212. layoutType: this.state.currentLayout,
  213. themeType: this.state.currentTheme,
  214. });
  215. const { customizedParams } = response.data;
  216. return customizedParams;
  217. }
  218. /**
  219. * Update behavior
  220. * @memberOf AdminCustomizeContainer
  221. * @return {string} Behavior
  222. */
  223. async updateCustomizeBehavior() {
  224. const response = await this.appContainer.apiv3.put('/customize-setting/behavior', {
  225. behaviorType: this.state.currentBehavior,
  226. });
  227. const { customizedParams } = response.data;
  228. return customizedParams;
  229. }
  230. /**
  231. * Update function
  232. * @memberOf AdminCustomizeContainer
  233. * @return {string} Functions
  234. */
  235. async updateCustomizeFunction() {
  236. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  237. isEnabledTimeline: this.state.isEnabledTimeline,
  238. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  239. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  240. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  241. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  242. });
  243. const { customizedParams } = response.data;
  244. return customizedParams;
  245. }
  246. /**
  247. * Update code highlight
  248. * @memberOf AdminCustomizeContainer
  249. * @return {Array} Code highlight
  250. */
  251. async updateHighlightJsStyle() {
  252. const response = await this.appContainer.apiv3.put('/customize-setting/highlight', {
  253. highlightJsStyle: this.state.currentHighlightJsStyleId,
  254. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  255. });
  256. const { customizedParams } = response.data;
  257. return customizedParams;
  258. }
  259. /**
  260. * Update customTitle
  261. * @memberOf AdminCustomizeContainer
  262. * @return {string} Customize title
  263. */
  264. async updateCustomizeTitle() {
  265. const response = await this.appContainer.apiv3.put('/customize-setting/customize-title', {
  266. customizeTitle: this.state.currentCustomizeTitle,
  267. });
  268. const { customizedParams } = response.data;
  269. return customizedParams;
  270. }
  271. /**
  272. * Update customHeader
  273. * @memberOf AdminCustomizeContainer
  274. * @return {string} Customize html header
  275. */
  276. async updateCustomizeHeader() {
  277. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  278. customizeHeader: this.state.currentCustomizeHeader,
  279. });
  280. const { customizedParams } = response.data;
  281. return customizedParams;
  282. }
  283. /**
  284. * Update customCss
  285. * @memberOf AdminCustomizeContainer
  286. * @return {string} Customize css
  287. */
  288. async updateCustomizeCss() {
  289. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  290. customizeCss: this.state.currentCustomizeCss,
  291. });
  292. const { customizedParams } = response.data;
  293. return customizedParams;
  294. }
  295. /**
  296. * Update customize script
  297. * @memberOf AdminCustomizeContainer
  298. * @return {string} Customize scripts
  299. */
  300. async updateCustomizeScript() {
  301. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  302. customizeScript: this.state.currentCustomizeScript,
  303. });
  304. const { customizedParams } = response.data;
  305. return customizedParams;
  306. }
  307. }