AdminCustomizeContainer.js 11 KB

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