UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

3 lines (2 loc) 7.06 kB
import{objectSpread2 as t,defineProperty as e}from"../../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{Canvas as i}from"../../canvas/Canvas.min.mjs";import{ITextClickBehavior as s}from"./ITextClickBehavior.min.mjs";import{keysMap as n,keysMapRtl as r,ctrlKeysMapDown as o,ctrlKeysMapUp as h}from"./constants.min.mjs";import{classRegistry as l}from"../../ClassRegistry.min.mjs";import{JUSTIFY as a,JUSTIFY_RIGHT as c,JUSTIFY_LEFT as d,JUSTIFY_CENTER as g}from"../Text/constants.min.mjs";import{RIGHT as f,LEFT as u,CENTER as p,FILL as C}from"../../constants.min.mjs";const x=t({selectionStart:0,selectionEnd:0,selectionColor:"rgba(17,119,255,0.3)",isEditing:!1,editable:!0,editingBorderColor:"rgba(102,153,255,0.25)",cursorWidth:2,cursorColor:"",cursorDelay:1e3,cursorDuration:600,caching:!0,hiddenTextareaContainer:null,keysMap:n,keysMapRtl:r,ctrlKeysMapDown:o,ctrlKeysMapUp:h},{_selectionDirection:null,_reSpace:/\s|\r?\n/,inCompositionMode:!1});class _ extends s{static getDefaults(){return t(t({},super.getDefaults()),_.ownDefaults)}get type(){const t=super.type;return"itext"===t?"i-text":t}constructor(e,i){super(e,t(t({},_.ownDefaults),i)),this.initBehavior()}_set(t,e){return this.isEditing&&this._savedProps&&t in this._savedProps?(this._savedProps[t]=e,this):("canvas"===t&&(this.canvas instanceof i&&this.canvas.textEditingManager.remove(this),e instanceof i&&e.textEditingManager.add(this)),super._set(t,e))}setSelectionStart(t){t=Math.max(t,0),this._updateAndFire("selectionStart",t)}setSelectionEnd(t){t=Math.min(t,this.text.length),this._updateAndFire("selectionEnd",t)}_updateAndFire(t,e){this[t]!==e&&(this._fireSelectionChanged(),this[t]=e),this._updateTextarea()}_fireSelectionChanged(){this.fire("selection:changed"),this.canvas&&this.canvas.fire("text:selection:changed",{target:this})}initDimensions(){this.isEditing&&this.initDelayedCursor(),super.initDimensions()}getSelectionStyles(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.selectionStart||0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.selectionEnd,i=arguments.length>2?arguments[2]:void 0;return super.getSelectionStyles(t,e,i)}setSelectionStyles(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.selectionStart||0,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:this.selectionEnd;return super.setSelectionStyles(t,e,i)}get2DCursorLocation(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.selectionStart,e=arguments.length>1?arguments[1]:void 0;return super.get2DCursorLocation(t,e)}render(t){super.render(t),this.cursorOffsetCache={},this.renderCursorOrSelection()}toCanvasElement(t){const e=this.isEditing;this.isEditing=!1;const i=super.toCanvasElement(t);return this.isEditing=e,i}renderCursorOrSelection(){if(!this.isEditing)return;const t=this.clearContextTop(!0);if(!t)return;const e=this._getCursorBoundaries();this.selectionStart===this.selectionEnd?this.renderCursor(t,e):this.renderSelection(t,e),this.canvas.contextTopDirty=!0,t.restore()}_getCursorBoundaries(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.selectionStart,e=arguments.length>1?arguments[1]:void 0;const i=this._getLeftOffset(),s=this._getTopOffset(),n=this._getCursorBoundariesOffsets(t,e);return{left:i,top:s,leftOffset:n.left,topOffset:n.top}}_getCursorBoundariesOffsets(t,e){return e?this.__getCursorBoundariesOffsets(t):this.cursorOffsetCache&&"top"in this.cursorOffsetCache?this.cursorOffsetCache:this.cursorOffsetCache=this.__getCursorBoundariesOffsets(t)}__getCursorBoundariesOffsets(t){let e=0,i=0;const{charIndex:s,lineIndex:n}=this.get2DCursorLocation(t);for(let t=0;t<n;t++)e+=this.getHeightOfLine(t);const r=this._getLineLeftOffset(n),o=this.__charBounds[n][s];o&&(i=o.left),0!==this.charSpacing&&s===this._textLines[n].length&&(i-=this._getWidthOfCharSpacing());const h={top:e,left:r+(i>0?i:0)};return"rtl"===this.direction&&(this.textAlign===f||this.textAlign===a||this.textAlign===c?h.left*=-1:this.textAlign===u||this.textAlign===d?h.left=r-(i>0?i:0):this.textAlign!==p&&this.textAlign!==g||(h.left=r-(i>0?i:0))),h}renderCursorAt(t){const e=this._getCursorBoundaries(t,!0);this._renderCursor(this.canvas.contextTop,e,t)}renderCursor(t,e){this._renderCursor(t,e,this.selectionStart)}_renderCursor(t,e,i){const s=this.get2DCursorLocation(i),n=s.lineIndex,r=s.charIndex>0?s.charIndex-1:0,o=this.getValueOfPropertyAt(n,r,"fontSize"),h=this.getObjectScaling().x*this.canvas.getZoom(),l=this.cursorWidth/h,a=this.getValueOfPropertyAt(n,r,"deltaY"),c=e.topOffset+(1-this._fontSizeFraction)*this.getHeightOfLine(n)/this.lineHeight-o*(1-this._fontSizeFraction);this.inCompositionMode&&this.renderSelection(t,e),t.fillStyle=this.cursorColor||this.getValueOfPropertyAt(n,r,C),t.globalAlpha=this._currentCursorOpacity,t.fillRect(e.left+e.leftOffset-l/2,c+e.top+a,l,o)}renderSelection(t,e){const i={selectionStart:this.inCompositionMode?this.hiddenTextarea.selectionStart:this.selectionStart,selectionEnd:this.inCompositionMode?this.hiddenTextarea.selectionEnd:this.selectionEnd};this._renderSelection(t,i,e)}renderDragSourceEffect(){const t=this.draggableTextDelegate.getDragStartSelection();this._renderSelection(this.canvas.contextTop,t,this._getCursorBoundaries(t.selectionStart,!0))}renderDropTargetEffect(t){const e=this.getSelectionStartFromPointer(t);this.renderCursorAt(e)}_renderSelection(t,e,i){const s=e.selectionStart,n=e.selectionEnd,r=this.textAlign.includes(a),o=this.get2DCursorLocation(s),h=this.get2DCursorLocation(n),l=o.lineIndex,C=h.lineIndex,x=o.charIndex<0?0:o.charIndex,_=h.charIndex<0?0:h.charIndex;for(let e=l;e<=C;e++){const s=this._getLineLeftOffset(e)||0;let n=this.getHeightOfLine(e),o=0,h=0,S=0;if(e===l&&(h=this.__charBounds[l][x].left),e>=l&&e<C)S=r&&!this.isEndOfWrapping(e)?this.width:this.getLineWidth(e)||5;else if(e===C)if(0===_)S=this.__charBounds[C][_].left;else{const t=this._getWidthOfCharSpacing();S=this.__charBounds[C][_-1].left+this.__charBounds[C][_-1].width-t}o=n,(this.lineHeight<1||e===C&&this.lineHeight>1)&&(n/=this.lineHeight);let m=i.left+s+h,O=n,v=0;const A=S-h;this.inCompositionMode?(t.fillStyle=this.compositionColor||"black",O=1,v=n):t.fillStyle=this.selectionColor,"rtl"===this.direction&&(this.textAlign===f||this.textAlign===a||this.textAlign===c?m=this.width-m-A:this.textAlign===u||this.textAlign===d?m=i.left+s-S:this.textAlign!==p&&this.textAlign!==g||(m=i.left+s-S)),t.fillRect(m,i.top+i.topOffset+v,A,O),i.topOffset+=o}}getCurrentCharFontSize(){const t=this._getCurrentCharIndex();return this.getValueOfPropertyAt(t.l,t.c,"fontSize")}getCurrentCharColor(){const t=this._getCurrentCharIndex();return this.getValueOfPropertyAt(t.l,t.c,C)}_getCurrentCharIndex(){const t=this.get2DCursorLocation(this.selectionStart,!0),e=t.charIndex>0?t.charIndex-1:0;return{l:t.lineIndex,c:e}}dispose(){this._exitEditing(),this.draggableTextDelegate.dispose(),super.dispose()}}e(_,"ownDefaults",x),e(_,"type","IText"),l.setClass(_),l.setClass(_,"i-text");export{_ as IText,x as iTextDefaultValues}; //# sourceMappingURL=IText.min.mjs.map