|
| 1 | +import styled from 'styled-components'; |
| 2 | +import * as txtgen from 'txtgen'; |
| 3 | +import { Component } from 'react'; |
| 4 | + |
| 5 | +import Text from '../../components/Text'; |
| 6 | +import Page from '../../components/Page'; |
| 7 | +import { H2 } from '../../components/Heading'; |
| 8 | +import { HR } from '../../components/Spacing'; |
| 9 | +import Section from '../../components/section/Section'; |
| 10 | +import { content, gutter } from '../../components/mixins'; |
| 11 | +import { zIndex } from '../../components/theme'; |
| 12 | + |
| 13 | +function onSelectionChange(component?: TextoPage) { |
| 14 | + console.log('-- \nselectionchange', component); |
| 15 | + |
| 16 | + const selection = window.getSelection(); |
| 17 | + const selectionRange = selection.getRangeAt(0); |
| 18 | + |
| 19 | + const rect = selectionRange.getBoundingClientRect(); |
| 20 | + const selectedText = selection.toString(); |
| 21 | + |
| 22 | + component.setState({ |
| 23 | + selectedText, |
| 24 | + menuTop: rect.top - rect.height - 30, |
| 25 | + menuLeft: rect.left, |
| 26 | + isMenuOpen: !!selectedText.trim(), |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +type SelectMenuProps = { |
| 31 | + top: number; |
| 32 | + left: number; |
| 33 | + isMenuOpen: boolean; |
| 34 | +}; |
| 35 | + |
| 36 | +const SelectMenu = styled<SelectMenuProps, 'div'>('div')` |
| 37 | + z-index: ${zIndex.dialog}; |
| 38 | +
|
| 39 | + top: ${({ top }) => top}px; |
| 40 | + left: ${({ left }) => left}px; |
| 41 | + position: fixed; |
| 42 | +
|
| 43 | + ${({ isMenuOpen }) => ` |
| 44 | + display: ${isMenuOpen ? 'block' : 'none'}; |
| 45 | + `} |
| 46 | +
|
| 47 | + color: snow; |
| 48 | + background: rgba(0,0,0,0.9); |
| 49 | + border-radius: 4px; |
| 50 | + ${gutter(0.25)} |
| 51 | +`; |
| 52 | + |
| 53 | +type State = { |
| 54 | + menuTop: SelectMenuProps['top']; |
| 55 | + menuLeft: SelectMenuProps['left']; |
| 56 | + isMenuOpen?: boolean; |
| 57 | + selectedText?: string; |
| 58 | +}; |
| 59 | + |
| 60 | +const contentText = txtgen.article(); |
| 61 | + |
| 62 | +class TextoPage extends Component<{}, State> { |
| 63 | + |
| 64 | + state: State = { |
| 65 | + menuTop: 0, |
| 66 | + menuLeft: 0, |
| 67 | + isMenuOpen: false, |
| 68 | + }; |
| 69 | + |
| 70 | + componentDidMount() { |
| 71 | + document.addEventListener('selectionchange', () => onSelectionChange(this)); |
| 72 | + } |
| 73 | + |
| 74 | + componentWillUnmount() { |
| 75 | + document.removeEventListener('selectionchange', () => onSelectionChange(this)); |
| 76 | + } |
| 77 | + |
| 78 | + render() { |
| 79 | + const { |
| 80 | + menuTop, |
| 81 | + menuLeft, |
| 82 | + isMenuOpen, |
| 83 | + selectedText, |
| 84 | + } = this.state; |
| 85 | + |
| 86 | + return ( |
| 87 | + <Page title="Text select"> |
| 88 | + <Section variation="secondary" mixins={[content()]}> |
| 89 | + <SelectMenu |
| 90 | + top={menuTop} |
| 91 | + left={menuLeft} |
| 92 | + isMenuOpen={isMenuOpen} |
| 93 | + > |
| 94 | + {selectedText} |
| 95 | + </SelectMenu> |
| 96 | + |
| 97 | + <H2> |
| 98 | + Texto |
| 99 | + </H2> |
| 100 | + <HR /> |
| 101 | + |
| 102 | + <Text> |
| 103 | + Text lookup and relationships to other applications is a rather{' '} |
| 104 | + normal feature in different platforms. Different content, like text, is{' '} |
| 105 | + forwared between applications either copying and pasting it. What if we can{' '} |
| 106 | + do the same with an arbitrary service? As much as there are privacy complications here{' '} |
| 107 | + there is also simple and interesting applications to it. |
| 108 | + </Text> |
| 109 | + |
| 110 | + <pre> |
| 111 | + What follows is random text, select words to see a response. |
| 112 | + </pre> |
| 113 | + |
| 114 | + <Text> |
| 115 | + {contentText} |
| 116 | + </Text> |
| 117 | + </Section> |
| 118 | + </Page> |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export default TextoPage; |
0 commit comments