AdminCustomizeContainer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import { isServer } from '@growi/core';
  2. import { Container } from 'unstated';
  3. import loggerFactory from '~/utils/logger';
  4. import { toastError } from '../util/apiNotification';
  5. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  6. // eslint-disable-next-line no-unused-vars
  7. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  8. /**
  9. * Service container for admin customize setting page (Customize.jsx)
  10. * @extends {Container} unstated Container
  11. */
  12. export default class AdminCustomizeContainer extends Container {
  13. constructor() {
  14. super();
  15. if (isServer()) {
  16. return;
  17. }
  18. this.state = {
  19. retrieveError: null,
  20. isEnabledTimeline: false,
  21. isSavedStatesOfTabChanges: false,
  22. isEnabledAttachTitleHeader: false,
  23. pageLimitationS: null,
  24. pageLimitationM: null,
  25. pageLimitationL: null,
  26. pageLimitationXL: null,
  27. isEnabledStaleNotification: false,
  28. isAllReplyShown: false,
  29. isSearchScopeChildrenAsDefault: false,
  30. currentHighlightJsStyleId: '',
  31. isHighlightJsStyleBorderEnabled: false,
  32. currentCustomizeTitle: '',
  33. currentCustomizeHeader: '',
  34. currentCustomizeCss: '',
  35. currentCustomizeScript: '',
  36. /* eslint-disable quote-props, no-multi-spaces */
  37. highlightJsCssSelectorOptions: {
  38. 'github': { name: '[Light] GitHub', border: false },
  39. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  40. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  41. 'xcode': { name: '[Light] Xcode', border: true },
  42. 'vs': { name: '[Light] Vs', border: true },
  43. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  44. 'hybrid': { name: '[Dark] Hybrid', border: false },
  45. 'monokai': { name: '[Dark] Monokai', border: false },
  46. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  47. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  48. },
  49. /* eslint-enable quote-props, no-multi-spaces */
  50. };
  51. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  52. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  53. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  54. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  55. }
  56. /**
  57. * Workaround for the mangling in production build to break constructor.name
  58. */
  59. static getClassName() {
  60. return 'AdminCustomizeContainer';
  61. }
  62. /**
  63. * retrieve customize data
  64. */
  65. async retrieveCustomizeData() {
  66. try {
  67. const response = await apiv3Get('/customize-setting/');
  68. const { customizeParams } = response.data;
  69. this.setState({
  70. isEnabledTimeline: customizeParams.isEnabledTimeline,
  71. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  72. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  73. pageLimitationS: customizeParams.pageLimitationS,
  74. pageLimitationM: customizeParams.pageLimitationM,
  75. pageLimitationL: customizeParams.pageLimitationL,
  76. pageLimitationXL: customizeParams.pageLimitationXL,
  77. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  78. isAllReplyShown: customizeParams.isAllReplyShown,
  79. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  80. currentHighlightJsStyleId: customizeParams.styleName,
  81. isHighlightJsStyleBorderEnabled: customizeParams.styleBorder,
  82. currentCustomizeTitle: customizeParams.customizeTitle,
  83. currentCustomizeHeader: customizeParams.customizeHeader,
  84. currentCustomizeCss: customizeParams.customizeCss,
  85. currentCustomizeScript: customizeParams.customizeScript,
  86. });
  87. // search style name from object for display
  88. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[customizeParams.styleName].name });
  89. }
  90. catch (err) {
  91. this.setState({ retrieveError: err });
  92. logger.error(err);
  93. throw new Error('Failed to fetch data');
  94. }
  95. }
  96. /**
  97. * Switch enabledTimeLine
  98. */
  99. switchEnableTimeline() {
  100. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  101. }
  102. /**
  103. * Switch savedStatesOfTabChanges
  104. */
  105. switchSavedStatesOfTabChanges() {
  106. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  107. }
  108. /**
  109. * Switch enabledAttachTitleHeader
  110. */
  111. switchEnabledAttachTitleHeader() {
  112. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  113. }
  114. /**
  115. * S: Switch pageListLimitationS
  116. */
  117. switchPageListLimitationS(value) {
  118. this.setState({ pageLimitationS: value });
  119. }
  120. /**
  121. * M: Switch pageListLimitationM
  122. */
  123. switchPageListLimitationM(value) {
  124. this.setState({ pageLimitationM: value });
  125. }
  126. /**
  127. * L: Switch pageListLimitationL
  128. */
  129. switchPageListLimitationL(value) {
  130. this.setState({ pageLimitationL: value });
  131. }
  132. /**
  133. * XL: Switch pageListLimitationXL
  134. */
  135. switchPageListLimitationXL(value) {
  136. this.setState({ pageLimitationXL: value });
  137. }
  138. /**
  139. * Switch enabledStaleNotification
  140. */
  141. switchEnableStaleNotification() {
  142. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  143. }
  144. /**
  145. * Switch isAllReplyShown
  146. */
  147. switchIsAllReplyShown() {
  148. this.setState({ isAllReplyShown: !this.state.isAllReplyShown });
  149. }
  150. /**
  151. * Switch isSearchScopeChildrenAsDefault
  152. */
  153. switchIsSearchScopeChildrenAsDefault() {
  154. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  155. }
  156. /**
  157. * Switch highlightJsStyle
  158. */
  159. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  160. this.setState({ currentHighlightJsStyleId: styleId });
  161. this.setState({ currentHighlightJsStyleName: styleName });
  162. // recommended settings are applied
  163. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  164. this.previewHighlightJsStyle(styleId);
  165. }
  166. /**
  167. * Switch highlightJsStyleBorder
  168. */
  169. switchHighlightJsStyleBorder() {
  170. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  171. }
  172. /**
  173. * Change customize Title
  174. */
  175. changeCustomizeTitle(inputValue) {
  176. this.setState({ currentCustomizeTitle: inputValue });
  177. }
  178. /**
  179. * Change customize Html header
  180. */
  181. changeCustomizeHeader(inputValue) {
  182. this.setState({ currentCustomizeHeader: inputValue });
  183. }
  184. /**
  185. * Change customize css
  186. */
  187. changeCustomizeCss(inputValue) {
  188. this.setState({ currentCustomizeCss: inputValue });
  189. }
  190. /**
  191. * Change customize script
  192. */
  193. changeCustomizeScript(inpuValue) {
  194. this.setState({ currentCustomizeScript: inpuValue });
  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 function
  208. * @memberOf AdminCustomizeContainer
  209. */
  210. async updateCustomizeFunction() {
  211. try {
  212. const response = await apiv3Put('/customize-setting/function', {
  213. isEnabledTimeline: this.state.isEnabledTimeline,
  214. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  215. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  216. pageLimitationS: this.state.pageLimitationS,
  217. pageLimitationM: this.state.pageLimitationM,
  218. pageLimitationL: this.state.pageLimitationL,
  219. pageLimitationXL: this.state.pageLimitationXL,
  220. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  221. isAllReplyShown: this.state.isAllReplyShown,
  222. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  223. });
  224. const { customizedParams } = response.data;
  225. this.setState({
  226. isEnabledTimeline: customizedParams.isEnabledTimeline,
  227. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  228. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  229. pageLimitationS: customizedParams.pageLimitationS,
  230. pageLimitationM: customizedParams.pageLimitationM,
  231. pageLimitationL: customizedParams.pageLimitationL,
  232. pageLimitationXL: customizedParams.pageLimitationXL,
  233. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  234. isAllReplyShown: customizedParams.isAllReplyShown,
  235. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  236. });
  237. }
  238. catch (err) {
  239. logger.error(err);
  240. throw new Error('Failed to update data');
  241. }
  242. }
  243. /**
  244. * Update code highlight
  245. * @memberOf AdminCustomizeContainer
  246. */
  247. async updateHighlightJsStyle() {
  248. try {
  249. const response = await apiv3Put('/customize-setting/highlight', {
  250. highlightJsStyle: this.state.currentHighlightJsStyleId,
  251. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  252. });
  253. const { customizedParams } = response.data;
  254. this.setState({
  255. highlightJsStyle: customizedParams.highlightJsStyle,
  256. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  257. });
  258. }
  259. catch (err) {
  260. logger.error(err);
  261. throw new Error('Failed to update data');
  262. }
  263. }
  264. /**
  265. * Update customTitle
  266. * @memberOf AdminCustomizeContainer
  267. */
  268. async updateCustomizeTitle() {
  269. try {
  270. const response = await apiv3Put('/customize-setting/customize-title', {
  271. customizeTitle: this.state.currentCustomizeTitle,
  272. });
  273. const { customizedParams } = response.data;
  274. this.setState({
  275. customizeTitle: customizedParams.customizeTitle,
  276. });
  277. }
  278. catch (err) {
  279. logger.error(err);
  280. throw new Error('Failed to update data');
  281. }
  282. }
  283. /**
  284. * Update customHeader
  285. * @memberOf AdminCustomizeContainer
  286. */
  287. async updateCustomizeHeader() {
  288. try {
  289. const response = await apiv3Put('/customize-setting/customize-header', {
  290. customizeHeader: this.state.currentCustomizeHeader,
  291. });
  292. const { customizedParams } = response.data;
  293. this.setState({
  294. currentCustomizeHeader: customizedParams.customizeHeader,
  295. });
  296. }
  297. catch (err) {
  298. logger.error(err);
  299. throw new Error('Failed to update data');
  300. }
  301. }
  302. /**
  303. * Update customCss
  304. * @memberOf AdminCustomizeContainer
  305. */
  306. async updateCustomizeCss() {
  307. try {
  308. const response = await apiv3Put('/customize-setting/customize-css', {
  309. customizeCss: this.state.currentCustomizeCss,
  310. });
  311. const { customizedParams } = response.data;
  312. this.setState({
  313. currentCustomizeCss: customizedParams.customizeCss,
  314. });
  315. }
  316. catch (err) {
  317. logger.error(err);
  318. throw new Error('Failed to update data');
  319. }
  320. }
  321. /**
  322. * Update customize script
  323. * @memberOf AdminCustomizeContainer
  324. * @return {string} Customize scripts
  325. */
  326. async updateCustomizeScript() {
  327. try {
  328. const response = await apiv3Put('/customize-setting/customize-script', {
  329. customizeScript: this.state.currentCustomizeScript,
  330. });
  331. const { customizedParams } = response.data;
  332. this.setState({
  333. currentCustomizeScript: customizedParams.customizeScript,
  334. });
  335. }
  336. catch (err) {
  337. logger.error(err);
  338. throw new Error('Failed to update data');
  339. }
  340. }
  341. }