import React, {Component, PureComponent} from 'react'; import {format_time,Time,TitleLine} from './infrastructure/widgets'; import {THUHOLE_API_ROOT} from './flows_api'; import './Common.css'; export {format_time,Time,TitleLine}; export const API_BASE=THUHOLE_API_ROOT+'services/thuhole'; // https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex function escape_regex(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } export function build_highlight_re(txt,split,option='g') { return txt ? new RegExp(`(${txt.split(split).filter((x)=>!!x).map(escape_regex).join('|')})`,option) : /^$/g; } export function ColoredSpan(props) { return ( {props.children} ) } export class HighlightedText extends PureComponent { render() { function normalize_url(url) { return /^https?:\/\//.test(url) ? url : 'http://'+url; } return (
{this.props.parts.map((part,idx)=>{
let [rule,p]=part;
return (
{
rule==='url_pid' ? /## :
rule==='url' ? {p} :
rule==='pid' ? {e.preventDefault(); this.props.show_pid(p.substring(1));}}>{p} :
rule==='nickname' ? {p} :
rule==='search' ? {p} :
p
}
);
})}
)
}
}
window.TEXTAREA_BACKUP={};
export class SafeTextarea extends Component {
constructor(props) {
super(props);
this.state={
text: '',
};
this.on_change_bound=this.on_change.bind(this);
this.on_keydown_bound=this.on_keydown.bind(this);
this.clear=this.clear.bind(this);
this.area_ref=React.createRef();
this.change_callback=props.on_change||(()=>{});
this.submit_callback=props.on_submit||(()=>{});
}
componentDidMount() {
this.setState({
text: window.TEXTAREA_BACKUP[this.props.id]||''
},()=>{
this.change_callback(this.state.text);
});
}
componentWillUnmount() {
window.TEXTAREA_BACKUP[this.props.id]=this.state.text;
this.change_callback(this.state.text);
}
on_change(event) {
this.setState({
text: event.target.value,
});
this.change_callback(event.target.value);
}
on_keydown(event) {
if(event.key==='Enter' && event.ctrlKey && !event.altKey) {
event.preventDefault();
this.submit_callback();
}
}
clear() {
this.setState({
text: '',
});
}
set(text) {
this.change_callback(text);
this.setState({
text: text,
});
}
get() {
return this.state.text;
}
focus() {
this.area_ref.current.focus();
}
render() {
return (
)
}
}
let pwa_prompt_event=null;
window.addEventListener('beforeinstallprompt', (e) => {
console.log('pwa: received before install prompt');
pwa_prompt_event=e;
});
export function PromotionBar(props) {
let is_ios=/iPhone|iPad|iPod/i.test(window.navigator.userAgent);
let is_installed=(window.matchMedia('(display-mode: standalone)').matches) || (window.navigator.standalone);
if(is_installed)
return null;
if(is_ios)
// noinspection JSConstructorReturnsPrimitive
return !navigator.standalone ? (