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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | 620× 620× 1005× 1× 2286× 2286× 819× 1467× 465× 1002× 1003× 939× 938× 24× 24× 937× 23× 23× 939× 25× 25× 2286× 2286× 1284× 1002× 1002× 1002× 63× 939× 939× 2× 2× 937× 937× 23× 23× 914× 1002× 2× 2× 2× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 2× 1004× 1× 1× 1× 1× 1× 1× 1× 1× 1× | import { annotationHelpers } from '../model' import Command from './Command' /** A class for commands intended to be executed on the annotations. See the example below to learn how to register an `AnnotationCommand` for a strong annotation. @class AnnotationCommand @extends ui/Command @example ```js import { AnnotationCommand } from 'substance' config.addCommand('strong', AnnotationCommand, {nodeType: 'strong'}) // Disable, when cursor is collapsed config.addCommand('strong', AnnotationCommand, { nodeType: 'strong', disableCollapsedCursor: true }) ``` */ class AnnotationCommand extends Command { constructor(...args) { super(...args) Iif (!this.config.nodeType) { throw new Error("'nodeType' is required") } } /** Get the type of an annotation. @returns {String} The annotation's type. */ getAnnotationType() { return this.config.nodeType } /** Get the annotation's data. @returns {Object} The annotation's data. */ getAnnotationData() { return {} } /** Checks if command couldn't be executed with current selection. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not command could be executed. */ isDisabled(sel, params) { let selectionState = params.selectionState // TODO: Container selections should be valid if the annotation type // is a container annotation. Currently we only allow property selections. if (!sel || sel.isNull() || !sel.isAttached() || sel.isCustomSelection()|| sel.isNodeSelection() || sel.isContainerSelection() || selectionState.isInlineNodeSelection()) { return true } if (this.config.disableCollapsedCursor && sel.isCollapsed()) { return true } return false } /** Checks if new annotations could be created. There should be no annotation overlapping, selection must be not collapsed. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not annotation could be created. */ // When there's no existing annotation overlapping, we create a new one. canCreate(annos, sel) { return (annos.length === 0 && !sel.isCollapsed()) } /** Checks if annotations could be fused. There should be more than one annotation overlaped by current selection. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not annotations could be fused. */ canFuse(annos, sel) { // When more than one annotation overlaps with the current selection return (annos.length >= 2 && !sel.isCollapsed()) } /** Checks if annotation could be deleted. Cursor or selection must be inside an existing annotation. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not annotation could be deleted. */ canDelete(annos, sel) { // When the cursor or selection is inside an existing annotation if (annos.length !== 1) return false let annoSel = annos[0].getSelection() return sel.isInsideOf(annoSel) } /** Checks if annotation could be expanded. There should be overlap with only a single annotation, selection should be also outside of this annotation. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not annotation could be expanded. */ canExpand(annos, sel) { // When there's some overlap with only a single annotation we do an expand if (annos.length !== 1) return false let annoSel = annos[0].getSelection() return sel.overlaps(annoSel) && !sel.isInsideOf(annoSel) } /** Checks if annotation could be truncated. There should be overlap with only a single annotation, selection should also have boundary in common with this annotation. @param {Array} annos annotations @param {Object} sel selection @returns {Boolean} Whether or not annotation could be truncated. */ canTruncate(annos, sel) { if (annos.length !== 1) return false let annoSel = annos[0].getSelection() return (sel.isLeftAlignedWith(annoSel) || sel.isRightAlignedWith(annoSel)) && !sel.contains(annoSel) && !sel.isCollapsed() } /** Gets command state object. @param {Object} state.selection the current selection @returns {Object} info object with command details. */ getCommandState(params) { // eslint-disable-line let sel = this._getSelection(params) // We can skip all checking if a disabled condition is met // E.g. we don't allow toggling of property annotations when current // selection is a container selection if (this.isDisabled(sel, params)) { return { disabled: true } } let annos = this._getAnnotationsForSelection(params) let newState = { disabled: false, active: false, mode: null } if (this.canCreate(annos, sel)) { newState.mode = 'create' } else Iif (this.canFuse(annos, sel)) { newState.mode = 'fuse' } else if (this.canTruncate(annos, sel)) { newState.active = true newState.mode = 'truncate' } else Iif (this.canExpand(annos, sel)) { newState.mode = 'expand' } else if (this.canDelete(annos, sel)) { newState.active = true newState.mode = 'delete' } else { newState.disabled = true } return newState } /** Execute command and trigger transformation. @returns {Object} info object with execution details. */ // Execute command and trigger transformations execute(params) { // Disabled the next line as I believe it is // always passed via params already // params.selection = this._getSelection(params) let commandState = params.commandState Iif (commandState.disabled) return false switch(commandState.mode) { case 'create': return this.executeCreate(params) case 'fuse': return this.executeFuse(params) case 'truncate': return this.executeTruncate(params) case 'expand': return this.executeExpand(params) case 'delete': return this.executeDelete(params) default: console.warn('Command.execute(): unknown mode', commandState.mode) return false } } executeCreate(params) { let annos = this._getAnnotationsForSelection(params) this._checkPrecondition(params, annos, this.canCreate) let editorSession = this._getEditorSession(params) let annoData = this.getAnnotationData() annoData.type = this.getAnnotationType() let anno editorSession.transaction((tx) => { anno = tx.annotate(annoData) }) return { mode: 'create', anno: anno } } executeFuse(params) { let annos = this._getAnnotationsForSelection(params); this._checkPrecondition(params, annos, this.canFuse); this._applyTransform(params, function(tx) { annotationHelpers.fuseAnnotation(tx, annos) }) return { mode: 'fuse', anno: annos[0] } } executeTruncate(params) { let annos = this._getAnnotationsForSelection(params) let anno = annos[0] this._checkPrecondition(params, annos, this.canTruncate) this._applyTransform(params, function(tx) { annotationHelpers.truncateAnnotation(tx, anno, params.selection) }) return { mode: 'truncate', anno: anno } } executeExpand(params) { let annos = this._getAnnotationsForSelection(params) let anno = annos[0] this._checkPrecondition(params, annos, this.canExpand) this._applyTransform(params, function(tx) { annotationHelpers.expandAnnotation(tx, anno, params.selection) }) return { mode: 'expand', anno: anno } } executeDelete(params) { let annos = this._getAnnotationsForSelection(params) let anno = annos[0] this._checkPrecondition(params, annos, this.canDelete) this._applyTransform(params, function(tx) { return tx.delete(anno.id) }) return { mode: 'delete', annoId: anno.id } } _checkPrecondition(params, annos, checker) { let sel = this._getSelection(params) Iif (!checker.call(this, annos, sel)) { throw new Error("AnnotationCommand: can't execute command for selection " + sel.toString()) } } _getAnnotationsForSelection(params) { return params.selectionState.getAnnotationsForType(this.getAnnotationType()) } /** Apply an annotation transformation. @returns {Object} transformed annotations. */ _applyTransform(params, transformFn) { let sel = this._getSelection(params) Iif (sel.isNull()) return let editorSession = this._getEditorSession(params) let result // to store transform result editorSession.setSelection(sel) editorSession.transaction(function(tx) { let out = transformFn(tx, params) Iif (out) result = out.result }) return result } } export default AnnotationCommand |