fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
2 lines • 7.36 kB
JavaScript
import{_defineProperty as e}from"../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.min.mjs";import{FILL as t}from"../../constants.min.mjs";import{classRegistry as n}from"../../ClassRegistry.min.mjs";import{createCanvasElementFor as r}from"../../util/misc/dom.min.mjs";import{JUSTIFY as i}from"../Text/constants.min.mjs";import{Canvas as a}from"../../canvas/Canvas.min.mjs";import{ITextClickBehavior as o}from"./ITextClickBehavior.min.mjs";import{ctrlKeysMapDown as s,ctrlKeysMapUp as c,keysMap as l,keysMapRtl as u}from"./constants.min.mjs";import{applyCanvasTransform as d}from"../../util/internals/applyCanvasTransform.min.mjs";const f={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:l,keysMapRtl:u,ctrlKeysMapDown:s,ctrlKeysMapUp:c,_selectionDirection:null,_reSpace:/\s|\r?\n/,inCompositionMode:!1};var p=class e extends o{static getDefaults(){return{...super.getDefaults(),...e.ownDefaults}}get type(){let e=super.type;return e===`itext`?`i-text`:e}constructor(t,n){super(t,{...e.ownDefaults,...n}),this.initBehavior()}_set(e,t){return this.isEditing&&this._savedProps&&e in this._savedProps?(this._savedProps[e]=t,this):(e===`canvas`&&(this.canvas instanceof a&&this.canvas.textEditingManager.remove(this),t instanceof a&&t.textEditingManager.add(this)),super._set(e,t))}setSelectionStart(e){e=Math.max(e,0),this._updateAndFire(`selectionStart`,e)}setSelectionEnd(e){e=Math.min(e,this.text.length),this._updateAndFire(`selectionEnd`,e)}_updateAndFire(e,t){this[e]!==t&&(this._fireSelectionChanged(),this[e]=t),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(e=this.selectionStart||0,t=this.selectionEnd,n){return super.getSelectionStyles(e,t,n)}setSelectionStyles(e,t=this.selectionStart||0,n=this.selectionEnd){return super.setSelectionStyles(e,t,n)}get2DCursorLocation(e=this.selectionStart,t){return super.get2DCursorLocation(e,t)}render(e){super.render(e),this.cursorOffsetCache={},this.renderCursorOrSelection()}toCanvasElement(e){let t=this.isEditing;this.isEditing=!1;let n=super.toCanvasElement(e);return this.isEditing=t,n}renderCursorOrSelection(){if(!this.isEditing||!this.canvas)return;let e=this.clearContextTop(!0);if(!e)return;let t=this._getCursorBoundaries(),n=this.findAncestorsWithClipPath(),i=n.length>0,a,o=e;if(i){a=r(e.canvas),o=a.getContext(`2d`),d(o,this.canvas);let t=this.calcTransformMatrix();o.transform(t[0],t[1],t[2],t[3],t[4],t[5])}if(this.selectionStart!==this.selectionEnd||this.inCompositionMode?this.renderSelection(o,t):this.renderCursor(o,t),i)for(let t of n){let n=t.clipPath,i=r(e.canvas),a=i.getContext(`2d`);if(d(a,this.canvas),!n.absolutePositioned){let e=t.calcTransformMatrix();a.transform(e[0],e[1],e[2],e[3],e[4],e[5])}n.transform(a),n.drawObject(a,!0,{}),this.drawClipPathOnCache(o,n,i)}i&&(e.setTransform(1,0,0,1,0,0),e.drawImage(a,0,0)),this.canvas.contextTopDirty=!0,e.restore()}findAncestorsWithClipPath(){let e=[],t=this;for(;t;)t.clipPath&&e.push(t),t=t.parent;return e}_getCursorBoundaries(e=this.selectionStart,t){let n=this._getLeftOffset(),r=this._getTopOffset(),i=this._getCursorBoundariesOffsets(e,t);return{left:n,top:r,leftOffset:i.left,topOffset:i.top}}_getCursorBoundariesOffsets(e,t){return t?this.__getCursorBoundariesOffsets(e):this.cursorOffsetCache&&`top`in this.cursorOffsetCache?this.cursorOffsetCache:this.cursorOffsetCache=this.__getCursorBoundariesOffsets(e)}__getCursorBoundariesOffsets(e){let t=0,n=0,{charIndex:r,lineIndex:i}=this.get2DCursorLocation(e),{textAlign:a,direction:o}=this;for(let e=0;e<i;e++)t+=this.getHeightOfLine(e);let s=this._getLineLeftOffset(i),c=this.__charBounds[i][r];c&&(n=c.left),this.charSpacing!==0&&r===this._textLines[i].length&&(n-=this._getWidthOfCharSpacing());let l=s+(n>0?n:0);return o===`rtl`&&(a===`right`||a===`justify`||a===`justify-right`?l*=-1:a===`left`||a===`justify-left`?l=s-(n>0?n:0):a!==`center`&&a!==`justify-center`||(l=s-(n>0?n:0))),{top:t,left:l}}renderCursorAt(e){this._renderCursor(this.canvas.contextTop,this._getCursorBoundaries(e,!0),e)}renderCursor(e,t){this._renderCursor(e,t,this.selectionStart)}getCursorRenderingData(e=this.selectionStart,t=this._getCursorBoundaries(e)){let n=this.get2DCursorLocation(e),r=n.lineIndex,i=n.charIndex>0?n.charIndex-1:0,a=this.getValueOfPropertyAt(r,i,`fontSize`),o=this.getObjectScaling().x*this.canvas.getZoom(),s=this.cursorWidth/o,c=this.getValueOfPropertyAt(r,i,`deltaY`),l=t.topOffset+(1-this._fontSizeFraction)*this.getHeightOfLine(r)/this.lineHeight-a*(1-this._fontSizeFraction);return{color:this.cursorColor||this.getValueOfPropertyAt(r,i,`fill`),opacity:this._currentCursorOpacity,left:t.left+t.leftOffset-s/2,top:l+t.top+c,width:s,height:a}}_renderCursor(e,t,n){let{color:r,opacity:i,left:a,top:o,width:s,height:c}=this.getCursorRenderingData(n,t);e.fillStyle=r,e.globalAlpha=i,e.fillRect(a,o,s,c)}renderSelection(e,t){let n={selectionStart:this.inCompositionMode?this.hiddenTextarea.selectionStart:this.selectionStart,selectionEnd:this.inCompositionMode?this.hiddenTextarea.selectionEnd:this.selectionEnd};this._renderSelection(e,n,t)}renderDragSourceEffect(){let e=this.draggableTextDelegate.getDragStartSelection();this._renderSelection(this.canvas.contextTop,e,this._getCursorBoundaries(e.selectionStart,!0))}renderDropTargetEffect(e){let t=this.getSelectionStartFromPointer(e);this.renderCursorAt(t)}_renderSelection(e,t,n){let{textAlign:r,direction:a}=this,o=t.selectionStart,s=t.selectionEnd,c=r.includes(i),l=this.get2DCursorLocation(o),u=this.get2DCursorLocation(s),d=l.lineIndex,f=u.lineIndex,p=l.charIndex<0?0:l.charIndex,m=u.charIndex<0?0:u.charIndex;for(let t=d;t<=f;t++){let i=this._getLineLeftOffset(t)||0,o=this.getHeightOfLine(t),s=0,l=0,u=0;if(t===d&&(l=this.__charBounds[d][p].left),t>=d&&t<f)u=c&&!this.isEndOfWrapping(t)?this.width:this.getLineWidth(t)||5;else if(t===f)if(m===0)u=this.__charBounds[f][m].left;else{let e=this._getWidthOfCharSpacing();u=this.__charBounds[f][m-1].left+this.__charBounds[f][m-1].width-e}s=o,(this.lineHeight<1||t===f&&this.lineHeight>1)&&(o/=this.lineHeight);let h=n.left+i+l,g=o,_=0,v=u-l;this.inCompositionMode?(e.fillStyle=this.compositionColor||`black`,g=1,_=o):e.fillStyle=this.selectionColor,a===`rtl`&&(r===`right`||r===`justify`||r===`justify-right`?h=this.width-h-v:r===`left`||r===`justify-left`?h=n.left+i-u:r!==`center`&&r!==`justify-center`||(h=n.left+i-u)),e.fillRect(h,n.top+n.topOffset+_,v,g),n.topOffset+=s}}getCurrentCharFontSize(){let e=this._getCurrentCharIndex();return this.getValueOfPropertyAt(e.l,e.c,`fontSize`)}getCurrentCharColor(){let e=this._getCurrentCharIndex();return this.getValueOfPropertyAt(e.l,e.c,t)}_getCurrentCharIndex(){let e=this.get2DCursorLocation(this.selectionStart,!0),t=e.charIndex>0?e.charIndex-1:0;return{l:e.lineIndex,c:t}}dispose(){this.exitEditingImpl(),this.draggableTextDelegate.dispose(),super.dispose()}};e(p,`ownDefaults`,f),e(p,`type`,`IText`),n.setClass(p),n.setClass(p,`i-text`);export{p as IText};
//# sourceMappingURL=IText.min.mjs.map