|
| 1 | +import React, { useCallback, useEffect, useMemo, useState } from 'react' |
| 2 | +import { createEditor } from 'slate' |
| 3 | +import { withHistory } from 'slate-history' |
| 4 | +import { Editable, Slate, withReact } from 'slate-react' |
| 5 | +import { css } from '@emotion/css' |
| 6 | + |
| 7 | +const TEST_CASES = [ |
| 8 | + { |
| 9 | + id: 'split-join', |
| 10 | + name: 'Split/Join', |
| 11 | + instructions: |
| 12 | + 'Hit enter twice then backspace twice in the following places:\n- Before "before"\n- Between the two "d"s in "middle"\n- After "after"', |
| 13 | + value: [ |
| 14 | + { |
| 15 | + type: 'paragraph', |
| 16 | + children: [ |
| 17 | + { text: 'One ' }, |
| 18 | + { text: 'before', bold: true }, |
| 19 | + { text: ' two ' }, |
| 20 | + { text: 'middle', bold: true }, |
| 21 | + { text: ' three ' }, |
| 22 | + { text: 'after', bold: true }, |
| 23 | + { text: ' four' }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + { |
| 29 | + id: 'insert', |
| 30 | + name: 'Insertion', |
| 31 | + instructions: |
| 32 | + 'Enter text below each line of instruction, including mis-spelling "wasnt"', |
| 33 | + value: [ |
| 34 | + { |
| 35 | + type: 'paragraph', |
| 36 | + children: [ |
| 37 | + { text: 'Type by tapping keys: ', bold: true }, |
| 38 | + { text: 'It wasnt me. No.' }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + { |
| 42 | + type: 'paragraph', |
| 43 | + children: [{ text: '' }], |
| 44 | + }, |
| 45 | + { |
| 46 | + type: 'paragraph', |
| 47 | + children: [ |
| 48 | + { text: 'Type using glide typing: ', bold: true }, |
| 49 | + { text: 'Yes Sam, I am.' }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + { |
| 53 | + type: 'paragraph', |
| 54 | + children: [{ text: '' }], |
| 55 | + }, |
| 56 | + { |
| 57 | + type: 'paragraph', |
| 58 | + children: [ |
| 59 | + { text: 'Type using voice input: ', bold: true }, |
| 60 | + { text: 'The quick brown fox jumps over the lazy dog' }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + type: 'paragraph', |
| 65 | + children: [{ text: '' }], |
| 66 | + }, |
| 67 | + { |
| 68 | + type: 'paragraph', |
| 69 | + children: [{ text: 'Write any two words using an IME', bold: true }], |
| 70 | + }, |
| 71 | + { |
| 72 | + type: 'paragraph', |
| 73 | + children: [{ text: '' }], |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + { |
| 78 | + id: 'special', |
| 79 | + name: 'Special', |
| 80 | + instructions: 'Follow the instructions on each line', |
| 81 | + value: [ |
| 82 | + { |
| 83 | + type: 'paragraph', |
| 84 | + children: [ |
| 85 | + { |
| 86 | + text: 'Type "it is", move cursor to "i|t" and hit enter.', |
| 87 | + bold: true, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + { |
| 92 | + type: 'paragraph', |
| 93 | + children: [{ text: '' }], |
| 94 | + }, |
| 95 | + { |
| 96 | + type: 'paragraph', |
| 97 | + children: [ |
| 98 | + { |
| 99 | + text: 'Move cursor to "mid|dle" and press space, backspace, space, backspace.', |
| 100 | + bold: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + { |
| 105 | + type: 'paragraph', |
| 106 | + children: [{ text: 'The middle word.' }], |
| 107 | + }, |
| 108 | + { |
| 109 | + type: 'paragraph', |
| 110 | + children: [ |
| 111 | + { |
| 112 | + text: 'Place cursor in line below. Wait for caps on keyboard to show up. If not try again. Type "It me. No." and check it doesn\'t mangle on the last period.', |
| 113 | + bold: true, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + { |
| 118 | + type: 'paragraph', |
| 119 | + children: [{ text: '' }], |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + { |
| 124 | + id: 'empty', |
| 125 | + name: 'Empty', |
| 126 | + instructions: |
| 127 | + 'Type "hello world", press enter, "hi", press enter, "bye", and then backspace over everything', |
| 128 | + value: [ |
| 129 | + { |
| 130 | + type: 'paragraph', |
| 131 | + children: [{ text: '' }], |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + { |
| 136 | + id: 'remove', |
| 137 | + name: 'Remove', |
| 138 | + instructions: |
| 139 | + 'Select from ANCHOR to FOCUS and press backspace. Move cursor to end. Backspace over all remaining content.', |
| 140 | + value: [ |
| 141 | + { |
| 142 | + type: 'paragraph', |
| 143 | + children: [ |
| 144 | + { text: 'Go and ' }, |
| 145 | + { text: 'select', bold: true }, |
| 146 | + { text: ' from this ANCHOR and then' }, |
| 147 | + ], |
| 148 | + }, |
| 149 | + { |
| 150 | + type: 'paragraph', |
| 151 | + children: [{ text: 'go and select' }], |
| 152 | + }, |
| 153 | + { |
| 154 | + type: 'paragraph', |
| 155 | + children: [ |
| 156 | + { text: 'to this FOCUS then press ' }, |
| 157 | + { text: 'backspace.', bold: true }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + { |
| 161 | + type: 'paragraph', |
| 162 | + children: [ |
| 163 | + { text: 'After you have done that move selection to very end.' }, |
| 164 | + ], |
| 165 | + }, |
| 166 | + { |
| 167 | + type: 'paragraph', |
| 168 | + children: [ |
| 169 | + { text: 'Then try ' }, |
| 170 | + { text: 'backspacing', bold: true }, |
| 171 | + { text: ' over all remaining text.' }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + ], |
| 175 | + }, |
| 176 | +] |
| 177 | +const AndroidTestsExample = () => { |
| 178 | + const [testId, setTestId] = useState( |
| 179 | + () => window.location.hash.replace('#', '') || TEST_CASES[0].id |
| 180 | + ) |
| 181 | + useEffect(() => { |
| 182 | + window.history.replaceState({}, '', `#${testId}`) |
| 183 | + }, [testId]) |
| 184 | + const testCase = TEST_CASES.find(({ id }) => id === testId) |
| 185 | + if (!testCase) { |
| 186 | + throw new Error(`Could not find test case '${testId}'`) |
| 187 | + } |
| 188 | + return ( |
| 189 | + <> |
| 190 | + <label> |
| 191 | + Test case:{' '} |
| 192 | + <select value={testId} onChange={e => setTestId(e.target.value)}> |
| 193 | + {TEST_CASES.map(({ name, id }) => ( |
| 194 | + <option key={id} value={id}> |
| 195 | + {name} |
| 196 | + </option> |
| 197 | + ))} |
| 198 | + </select> |
| 199 | + </label> |
| 200 | + |
| 201 | + <p |
| 202 | + className={css` |
| 203 | + font-weight: 600; |
| 204 | + margin: 0.5rem 0; |
| 205 | + white-space: pre-line; |
| 206 | + `} |
| 207 | + > |
| 208 | + {testCase.instructions} |
| 209 | + </p> |
| 210 | + |
| 211 | + <TestCase key={testId} {...testCase} /> |
| 212 | + </> |
| 213 | + ) |
| 214 | +} |
| 215 | +const TestCase = ({ value }) => { |
| 216 | + const renderLeaf = useCallback(props => <Leaf {...props} />, []) |
| 217 | + const editor = useMemo(() => withHistory(withReact(createEditor())), []) |
| 218 | + return ( |
| 219 | + <Slate editor={editor} initialValue={value}> |
| 220 | + <Editable |
| 221 | + renderLeaf={renderLeaf} |
| 222 | + placeholder="Enter some text…" |
| 223 | + spellCheck |
| 224 | + /> |
| 225 | + </Slate> |
| 226 | + ) |
| 227 | +} |
| 228 | +const Leaf = ({ attributes, children, leaf }) => { |
| 229 | + if (leaf.bold) { |
| 230 | + children = <strong>{children}</strong> |
| 231 | + } |
| 232 | + return <span {...attributes}>{children}</span> |
| 233 | +} |
| 234 | +export default AndroidTestsExample |
0 commit comments