import React, { PureComponent } from 'react'; import './Config.css'; const BUILTIN_IMGS = { 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/gbp.jpg': '怀旧背景(默认)', 'https://www.tsinghua.edu.cn/image/nav-bg.jpg': '清华紫', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/gbp.jpg': '寻觅繁星', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/eriri.jpg': '平成著名画师', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/yurucamp.jpg': '露营天下第一', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/minecraft.jpg': '麦恩·库拉夫特', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/cyberpunk.jpg': '赛博城市', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/bj.jpg': '城市的星光', 'https://cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/sif.jpg': '梦开始的地方', }; const DEFAULT_CONFIG = { background_img: '//cdn.jsdelivr.net/gh/thuhole/webhole@gh-pages/static/bg/gbp.jpg', background_color: '#113366', pressure: false, easter_egg: true, color_scheme: 'default', block_words_v2: ['#天火', '#桃花石'], whitelist_cw: [], ipfs_gateway: ['https://.ipfs.dweb.link/'], }; export function load_config() { let config = Object.assign({}, DEFAULT_CONFIG); let loaded_config; try { loaded_config = JSON.parse(localStorage['hole_config'] || '{}'); } catch (e) { alert('设置加载失败,将重置为默认设置!\n' + e); delete localStorage['hole_config']; loaded_config = {}; } // unrecognized configs are removed Object.keys(loaded_config).forEach((key) => { if (config[key] !== undefined) config[key] = loaded_config[key]; }); if (loaded_config['block_words']) { config['block_words_v2'] = loaded_config['block_words'].concat( config['block_words_v2'], ); } console.log('config loaded', config); window.config = config; } export function save_config() { localStorage['hole_config'] = JSON.stringify(window.config); load_config(); } export function bgimg_style(img, color) { if (img === undefined) img = window.config.background_img; if (color === undefined) color = window.config.background_color; return { background: 'transparent center center', backgroundImage: img === null ? 'unset' : 'url("' + encodeURI(img) + '")', backgroundColor: color, backgroundSize: 'cover', }; } class ConfigBackground extends PureComponent { constructor(props) { super(props); this.state = { img: window.config.background_img, color: window.config.background_color, }; } save_changes() { this.props.callback({ background_img: this.state.img, background_color: this.state.color, }); } on_select(e) { let value = e.target.value; this.setState( { img: value === '##other' ? '' : value === '##color' ? null : value, }, this.save_changes.bind(this), ); } on_change_img(e) { this.setState( { img: e.target.value, }, this.save_changes.bind(this), ); } on_change_color(e) { this.setState( { color: e.target.value, }, this.save_changes.bind(this), ); } render() { let img_select = this.state.img === null ? '##color' : Object.keys(BUILTIN_IMGS).indexOf(this.state.img) === -1 ? '##other' : this.state.img; return (

背景图片:   #background_img  {img_select === '##other' && ( )} {img_select === '##color' && ( )}

); } } class ConfigColorScheme extends PureComponent { constructor(props) { super(props); this.state = { color_scheme: window.config.color_scheme, }; } save_changes() { this.props.callback({ color_scheme: this.state.color_scheme, }); } on_select(e) { let value = e.target.value; this.setState( { color_scheme: value, }, this.save_changes.bind(this), ); } render() { return (

夜间模式:  #color_scheme

选择浅色或深色模式,深色模式下将会调暗图片亮度

); } } class ConfigTextArea extends PureComponent { constructor(props) { super(props); this.state = { [props.id]: window.config[props.id], }; } save_changes() { this.props.callback({ [this.props.id]: this.props.sift(this.state[this.props.id]), }); } on_change(e) { let value = this.props.parse(e.target.value); this.setState( { [this.props.id]: value, }, this.save_changes.bind(this), ); } render() { return (