|
1 | 1 | /*eslint no-unused-vars: 0*/ |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | | -import React, {PureComponent} from 'react' |
| 4 | +import React, { PureComponent } from 'react' |
5 | 5 | import PropTypes from 'prop-types' |
6 | 6 | import History from './history' |
7 | | -import {uuid4} from './utils' |
| 7 | +import { uuid4 } from './utils' |
8 | 8 | import Select from './select' |
9 | 9 | import Pencil from './pencil' |
10 | 10 | import Line from './line' |
@@ -124,12 +124,28 @@ class SketchField extends PureComponent { |
124 | 124 | }); |
125 | 125 | }; |
126 | 126 |
|
| 127 | + addText = (text, options = {}) => { |
| 128 | + let canvas = this._fc; |
| 129 | + let iText = new fabric.IText(text, options); |
| 130 | + let opts = { |
| 131 | + left: (canvas.getWidth() - iText.width) * 0.5, |
| 132 | + top: (canvas.getHeight() - iText.height) * 0.5, |
| 133 | + }; |
| 134 | + Object.assign(options, opts); |
| 135 | + iText.set({ |
| 136 | + 'left': options.left, |
| 137 | + 'top': options.top |
| 138 | + }) |
| 139 | + |
| 140 | + canvas.add(iText); |
| 141 | + } |
| 142 | + |
127 | 143 | /** |
128 | 144 | * Action when an object is added to the canvas |
129 | 145 | */ |
130 | 146 | _onObjectAdded = (e) => { |
131 | 147 | if (!this.state.action) { |
132 | | - this.setState({action: true}); |
| 148 | + this.setState({ action: true }); |
133 | 149 | return |
134 | 150 | } |
135 | 151 | let obj = e.target; |
@@ -206,9 +222,9 @@ class SketchField extends PureComponent { |
206 | 222 | */ |
207 | 223 | _resize = (e) => { |
208 | 224 | if (e) e.preventDefault(); |
209 | | - let {widthCorrection, heightCorrection} = this.props; |
| 225 | + let { widthCorrection, heightCorrection } = this.props; |
210 | 226 | let canvas = this._fc; |
211 | | - let {offsetWidth, clientHeight} = this._container; |
| 227 | + let { offsetWidth, clientHeight } = this._container; |
212 | 228 | let prevWidth = canvas.getWidth(); |
213 | 229 | let prevHeight = canvas.getHeight(); |
214 | 230 | let wfactor = ((offsetWidth - widthCorrection) / prevWidth).toFixed(2); |
@@ -308,7 +324,7 @@ class SketchField extends PureComponent { |
308 | 324 | //noinspection Eslint |
309 | 325 | let [obj, prevState, currState] = history.redo(); |
310 | 326 | if (obj.version === 0) { |
311 | | - this.setState({action: false}, () => { |
| 327 | + this.setState({ action: false }, () => { |
312 | 328 | canvas.add(obj); |
313 | 329 | obj.version = 1 |
314 | 330 | }) |
@@ -474,6 +490,11 @@ class SketchField extends PureComponent { |
474 | 490 | canvas.on('object:moving', this._onObjectMoving); |
475 | 491 | canvas.on('object:scaling', this._onObjectScaling); |
476 | 492 | canvas.on('object:rotating', this._onObjectRotating); |
| 493 | + // IText Events fired on Adding Text |
| 494 | + // canvas.on("text:event:changed", console.log) |
| 495 | + // canvas.on("text:selection:changed", console.log) |
| 496 | + // canvas.on("text:editing:entered", console.log) |
| 497 | + // canvas.on("text:editing:exited", console.log) |
477 | 498 |
|
478 | 499 | this.disableTouchScroll(); |
479 | 500 |
|
@@ -554,8 +575,8 @@ class SketchField extends PureComponent { |
554 | 575 | } = this.props; |
555 | 576 |
|
556 | 577 | let canvasDivStyle = Object.assign({}, style ? style : {}, |
557 | | - width ? {width: width} : {}, |
558 | | - height ? {height: height} : {height: 512}); |
| 578 | + width ? { width: width } : {}, |
| 579 | + height ? { height: height } : { height: 512 }); |
559 | 580 |
|
560 | 581 | return ( |
561 | 582 | <div |
|
0 commit comments