Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import React, {Component} from 'react'; import {connect} from 'react-redux'; import PropTypes from 'prop-types/prop-types'; import ContentEditable from './ContentEditable'; import SHARED from '../shared'; import classNames from 'classnames'; import {insert, activateByNid, Target} from '../actions'; import {say} from '../utils'; import CodeMirror from 'codemirror'; class NodeEditable extends Component { static defaultProps = { children: null, } static propTypes = { target: PropTypes.instanceOf(Target), children: PropTypes.node, isInsertion: PropTypes.bool.isRequired, value: PropTypes.string, dispatch: PropTypes.func, setErrorId: PropTypes.func, onChange: PropTypes.func, onDisableEditable: PropTypes.func, clearSelections: PropTypes.func, focusSelf: PropTypes.func, isErrored: PropTypes.bool, contentEditableProps: PropTypes.object, extraClasses: PropTypes.array, } constructor(props) { super(props); const {value, dispatch} = this.props; if (value === null) { dispatch((_, getState) => { const {ast} = getState(); const {target} = this.props; this.cachedValue = target.getText(ast); }); } } saveEdit = e => { e.stopPropagation(); const {target, setErrorId, onChange, onDisableEditable, dispatch} = this.props; dispatch((dispatch, getState) => { const {focusId, ast} = getState(); //console.log('XXX NodeEditable:50 focusId from state=', focusId); if (this.props.value === null || this.props.value === this.cachedValue) { //console.log('XXX NodeEditable:53'); this.props.onDisableEditable(false); const focusNode = ast.getNodeById(focusId); //const nid = ast.getNodeById(focusId).nid; const nid = focusNode && focusNode.nid; //console.log('XXX NodeEditable:58 nid=', nid); dispatch(activateByNid(nid, true)); //console.log('XXX NodeEditable:60'); return; } const value = this.props.value; let annt = `${this.props.isInsertion ? 'inserted' : 'changed'} ${value}`; const onSuccess = ({firstNewId}) => { //console.log('XXX NodeEditable:67, onSuccess of focusId=', focusId, 'nid=', firstNewId); if (firstNewId !== null && firstNewId !== undefined) { //console.log('XXX NodeEditable:69'); const {ast} = getState(); const firstNewNid = ast.getNodeById(focusId).nid; //console.log('XXX NodeEditable:72 aBNid of focusId=', focusId, 'nid=', firstNewNid); dispatch(activateByNid(firstNewNid, {allowMove: true})); } else { //console.log('XXX NodeEditable:75 aBNid of null') dispatch(activateByNid(null, {allowMove: false})); } onChange(null); onDisableEditable(false); setErrorId(''); say(annt); }; const onError = e => { const errorText = SHARED.parser.getExceptionMessage(e); console.log(errorText); this.ignoreBlur = false; setErrorId(target.node ? target.node.id : 'editing'); this.setSelection(false); }; insert(value, target, onSuccess, onError, annt); }); } handleKeyDown = e => { switch (CodeMirror.keyName(e)) { case 'Enter': { this.ignoreBlur = true; this.saveEdit(e); return; } case 'Alt-Q': case 'Esc': this.ignoreBlur = true; e.stopPropagation(); this.props.onChange(null); this.props.onDisableEditable(false); this.props.setErrorId(''); setTimeout(() => this.props.focusSelf(), 200); return; } } suppressEvent = e => { e.stopPropagation(); } componentDidMount() { const text = this.props.value !== null ? this.props.value : this.cachedValue; const annt = (this.props.isInsertion ? 'inserting' : 'editing') + ` ${text}`; say(annt + `. Use Enter to save, and Alt-Q to cancel`); this.props.clearSelections(); } /* * No need to reset text because we assign new key (via the parser + patching) * to changed nodes, so they will be completely unmounted and mounted back * with correct values. */ handleBlur = e => { if (this.ignoreBlur) return; this.saveEdit(e); } setSelection = isCollapsed => { setTimeout(() => { const range = document.createRange(); range.selectNodeContents(this.element); if (isCollapsed) range.collapse(false); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); }, 20); } contentEditableDidMount = el => { this.element = el; this.setSelection(this.props.isInsertion); } render() { const { contentEditableProps, extraClasses, value, onChange, } = this.props; const classes = [ 'blocks-literal', 'quarantine', 'blocks-editing', 'blocks-node', {'blocks-error': this.props.isErrored}, ].concat(extraClasses); const text = value !== null ? value : this.cachedValue; return ( <ContentEditable {...contentEditableProps} className = {classNames(classes)} role = "textbox" itDidMount = {this.contentEditableDidMount} onChange = {onChange} onBlur = {this.handleBlur} onKeyDown = {this.handleKeyDown} // trap mousedown, clicks and doubleclicks, to prevent focus change, or // parent nodes from toggling collapsed state onMouseDown= {this.suppressEvent} onClick = {this.suppressEvent} onDoubleClick = {this.suppressEvent} aria-label = {text} value = {text} /> ); } } const mapStateToProps = ({cm, errorId}, {target}) => { const nodeId = target.node ? target.node.id : 'editing'; const isErrored = errorId == nodeId; return {cm, isErrored}; }; const mapDispatchToProps = dispatch => ({ dispatch, setErrorId: errorId => dispatch({type: 'SET_ERROR_ID', errorId}), focusSelf: () => dispatch(activateByNid(null, {allowMove: false})), clearSelections: () => dispatch({type: 'SET_SELECTIONS', selections: []}), }); export default connect(mapStateToProps, mapDispatchToProps)(NodeEditable); |