AdminCustomizeContainer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. let assetPath;
  185. try {
  186. // get theme asset path
  187. const response = await this.appContainer.apiv3.get('/customize-setting/layout-theme/asset-path', { themeName });
  188. assetPath = response.data.assetPath;
  189. }
  190. catch (err) {
  191. toastError(err);
  192. }
  193. const themeLink = document.getElementById('grw-theme-link');
  194. themeLink.setAttribute('href', assetPath);
  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. * @return {Array} Appearance
  210. */
  211. async updateCustomizeLayoutAndTheme() {
  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. return customizedParams;
  218. }
  219. /**
  220. * Update behavior
  221. * @memberOf AdminCustomizeContainer
  222. * @return {string} Behavior
  223. */
  224. async updateCustomizeBehavior() {
  225. const response = await this.appContainer.apiv3.put('/customize-setting/behavior', {
  226. behaviorType: this.state.currentBehavior,
  227. });
  228. const { customizedParams } = response.data;
  229. return customizedParams;
  230. }
  231. /**
  232. * Update function
  233. * @memberOf AdminCustomizeContainer
  234. * @return {string} Functions
  235. */
  236. async updateCustomizeFunction() {
  237. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  238. isEnabledTimeline: this.state.isEnabledTimeline,
  239. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  240. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  241. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  242. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  243. });
  244. const { customizedParams } = response.data;
  245. return customizedParams;
  246. }
  247. /**
  248. * Update code highlight
  249. * @memberOf AdminCustomizeContainer
  250. * @return {Array} Code highlight
  251. */
  252. async updateHighlightJsStyle() {
  253. const response = await this.appContainer.apiv3.put('/customize-setting/highlight', {
  254. highlightJsStyle: this.state.currentHighlightJsStyleId,
  255. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  256. });
  257. const { customizedParams } = response.data;
  258. return customizedParams;
  259. }
  260. /**
  261. * Update customTitle
  262. * @memberOf AdminCustomizeContainer
  263. * @return {string} Customize title
  264. */
  265. async updateCustomizeTitle() {
  266. const response = await this.appContainer.apiv3.put('/customize-setting/customize-title', {
  267. customizeTitle: this.state.currentCustomizeTitle,
  268. });
  269. const { customizedParams } = response.data;
  270. return customizedParams;
  271. }
  272. /**
  273. * Update customHeader
  274. * @memberOf AdminCustomizeContainer
  275. * @return {string} Customize html header
  276. */
  277. async updateCustomizeHeader() {
  278. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  279. customizeHeader: this.state.currentCustomizeHeader,
  280. });
  281. const { customizedParams } = response.data;
  282. return customizedParams;
  283. }
  284. /**
  285. * Update customCss
  286. * @memberOf AdminCustomizeContainer
  287. * @return {string} Customize css
  288. */
  289. async updateCustomizeCss() {
  290. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  291. customizeCss: this.state.currentCustomizeCss,
  292. });
  293. const { customizedParams } = response.data;
  294. return customizedParams;
  295. }
  296. /**
  297. * Update customize script
  298. * @memberOf AdminCustomizeContainer
  299. * @return {string} Customize scripts
  300. */
  301. async updateCustomizeScript() {
  302. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  303. customizeScript: this.state.currentCustomizeScript,
  304. });
  305. const { customizedParams } = response.data;
  306. return customizedParams;
  307. }
  308. }