AdminCustomizeContainer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. 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 behaviorType
  106. */
  107. switchBehaviorType(behaviorName) {
  108. this.setState({ currentBehavior: behaviorName });
  109. }
  110. /**
  111. * Switch enabledTimeLine
  112. */
  113. switchEnableTimeline() {
  114. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  115. }
  116. /**
  117. * Switch savedStatesOfTabChanges
  118. */
  119. switchSavedStatesOfTabChanges() {
  120. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  121. }
  122. /**
  123. * Switch enabledAttachTitleHeader
  124. */
  125. switchEnabledAttachTitleHeader() {
  126. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  127. }
  128. /**
  129. * Switch recentCreatedLimit
  130. */
  131. switchRecentCreatedLimit(value) {
  132. this.setState({ currentRecentCreatedLimit: value });
  133. }
  134. /**
  135. * Switch enabledStaleNotification
  136. */
  137. switchEnableStaleNotification() {
  138. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  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 behavior
  229. * @memberOf AdminCustomizeContainer
  230. */
  231. async updateCustomizeBehavior() {
  232. try {
  233. const response = await this.appContainer.apiv3.put('/customize-setting/behavior', {
  234. behaviorType: this.state.currentBehavior,
  235. });
  236. const { customizedParams } = response.data;
  237. this.setState({
  238. behaviorType: customizedParams.behaviorType,
  239. });
  240. }
  241. catch (err) {
  242. logger.error(err);
  243. throw new Error('Failed to update data');
  244. }
  245. }
  246. /**
  247. * Update function
  248. * @memberOf AdminCustomizeContainer
  249. */
  250. async updateCustomizeFunction() {
  251. try {
  252. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  253. isEnabledTimeline: this.state.isEnabledTimeline,
  254. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  255. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  256. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  257. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  258. });
  259. const { customizedParams } = response.data;
  260. this.setState({
  261. isEnabledTimeline: customizedParams.isEnabledTimeline,
  262. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  263. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  264. recentCreatedLimit: customizedParams.currentRecentCreatedLimit,
  265. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  266. });
  267. }
  268. catch (err) {
  269. logger.error(err);
  270. throw new Error('Failed to update data');
  271. }
  272. }
  273. /**
  274. * Update code highlight
  275. * @memberOf AdminCustomizeContainer
  276. */
  277. async updateHighlightJsStyle() {
  278. try {
  279. const response = await this.appContainer.apiv3.put('/customize-setting/highlight', {
  280. highlightJsStyle: this.state.currentHighlightJsStyleId,
  281. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  282. });
  283. const { customizedParams } = response.data;
  284. this.setState({
  285. highlightJsStyle: customizedParams.highlightJsStyle,
  286. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  287. });
  288. }
  289. catch (err) {
  290. logger.error(err);
  291. throw new Error('Failed to update data');
  292. }
  293. }
  294. /**
  295. * Update customTitle
  296. * @memberOf AdminCustomizeContainer
  297. */
  298. async updateCustomizeTitle() {
  299. try {
  300. const response = await this.appContainer.apiv3.put('/customize-setting/customize-title', {
  301. customizeTitle: this.state.currentCustomizeTitle,
  302. });
  303. const { customizedParams } = response.data;
  304. this.setState({
  305. customizeTitle: customizedParams.customizeTitle,
  306. });
  307. }
  308. catch (err) {
  309. logger.error(err);
  310. throw new Error('Failed to update data');
  311. }
  312. }
  313. /**
  314. * Update customHeader
  315. * @memberOf AdminCustomizeContainer
  316. */
  317. async updateCustomizeHeader() {
  318. try {
  319. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  320. customizeHeader: this.state.currentCustomizeHeader,
  321. });
  322. const { customizedParams } = response.data;
  323. this.setState({
  324. currentCustomizeHeader: customizedParams.customizeHeader,
  325. });
  326. }
  327. catch (err) {
  328. logger.error(err);
  329. throw new Error('Failed to update data');
  330. }
  331. }
  332. /**
  333. * Update customCss
  334. * @memberOf AdminCustomizeContainer
  335. */
  336. async updateCustomizeCss() {
  337. try {
  338. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  339. customizeCss: this.state.currentCustomizeCss,
  340. });
  341. const { customizedParams } = response.data;
  342. this.setState({
  343. currentCustomizeCss: customizedParams.customizeCss,
  344. });
  345. }
  346. catch (err) {
  347. logger.error(err);
  348. throw new Error('Failed to update data');
  349. }
  350. }
  351. /**
  352. * Update customize script
  353. * @memberOf AdminCustomizeContainer
  354. * @return {string} Customize scripts
  355. */
  356. async updateCustomizeScript() {
  357. try {
  358. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  359. customizeScript: this.state.currentCustomizeScript,
  360. });
  361. const { customizedParams } = response.data;
  362. this.setState({
  363. currentCustomizeScript: customizedParams.customizeScript,
  364. });
  365. }
  366. catch (err) {
  367. logger.error(err);
  368. throw new Error('Failed to update data');
  369. }
  370. }
  371. }