Skip to content

Commit

Permalink
ULMS-1203 Fixed bug with textbox on page zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Mar 29, 2021
1 parent 428e8e8 commit 332466f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulms/components",
"version": "1.3.0",
"version": "1.3.1",
"description": "UI components library",
"homepage": "https://github.com/netology-group/ulms-media-ui",
"bugs": {
Expand Down
4 changes: 3 additions & 1 deletion packages/drawing/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { Drawing, penToolModeEnum, shapeToolModeEnum, toolEnum } from './src/drawing'
export { penToolModeEnum, shapeToolModeEnum, toolEnum } from './src/constants'

export { Drawing } from './src/drawing'

export { LockProvider } from './src/lock-provider'

Expand Down
2 changes: 1 addition & 1 deletion packages/drawing/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/drawing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulms/ui-drawing",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"keywords": [
"lerna"
Expand Down Expand Up @@ -32,6 +32,7 @@
"react": "^16.12.0"
},
"peerDependencies": {
"fabric": "netology-group/fabric.js#feature/ULMS-509",
"react": "^15.6 || ^16.x || ^17.x"
}
}
31 changes: 31 additions & 0 deletions packages/drawing/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const toolEnum = {
ERASER: 'eraser',
PAN: 'pan',
PEN: 'pen',
SELECT: 'select',
SHAPE: 'shape',
TEXT: 'textbox',
}

export const penToolModeEnum = {
PENCIL: 'pencil',
MARKER: 'marker',
LINE: 'line',
}

export const shapeToolModeEnum = {
CIRCLE: 'circle',
CIRCLE_SOLID: 'circle-solid',
RECT: 'rect',
RECT_SOLID: 'rect-solid',
TRIANGLE: 'triangle',
TRIANGLE_SOLID: 'triangle-solid',
}

export const enhancedFields = [
'_id',
'_lockedbyuser',
'_lockedselection',
'noScaleCache',
'strokeUniform',
]
33 changes: 1 addition & 32 deletions packages/drawing/src/drawing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fabric } from 'fabric'
import { queue as Queue } from 'd3-queue'
import Hammer from 'hammerjs'

import { enhancedFields, penToolModeEnum, shapeToolModeEnum, toolEnum } from './constants'
import { toCSSColor } from './util/to-css-color'
import { LockProvider } from './lock-provider'

Expand All @@ -30,38 +31,6 @@ import {
triangleSolid,
} from './tools/_shapes'

export const toolEnum = {
ERASER: 'eraser',
PAN: 'pan',
PEN: 'pen',
SELECT: 'select',
SHAPE: 'shape',
TEXT: 'textbox',
}

export const penToolModeEnum = {
PENCIL: 'pencil',
MARKER: 'marker',
LINE: 'line',
}

export const shapeToolModeEnum = {
CIRCLE: 'circle',
CIRCLE_SOLID: 'circle-solid',
RECT: 'rect',
RECT_SOLID: 'rect-solid',
TRIANGLE: 'triangle',
TRIANGLE_SOLID: 'triangle-solid',
}

export const enhancedFields = [
'_id',
'_lockedbyuser',
'_lockedselection',
'noScaleCache',
'strokeUniform',
]

export const normalizeFields = (object, fields) => Object.assign(
object,
fields.reduce((a, field) => {
Expand Down
28 changes: 28 additions & 0 deletions packages/drawing/src/select-tool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Adapter from 'enzyme-adapter-react-16' // eslint-disable-line import/no-e

import { Drawing, LockProvider, toolEnum } from '../index'

import SelectTool from './tools/select'

Enzyme.configure({ adapter: new Adapter() })

const { shallow } = Enzyme
Expand Down Expand Up @@ -114,4 +116,30 @@ describe('`updateAllSelection` on onlineIds change is ok', () => {
},
])
})

it('`handleTextEditStartEvent` sets style for hiddenTextarea', () => {
const wrap = shallow((
<Drawing
tokenProvider={tokenProvider}
objects={[]}
tool={toolEnum.SELECT}
/>
))

const instance = wrap.instance()
const st = new SelectTool(instance.canvas, {})
const opts = {
target: {
hiddenTextarea: {
style: {},
},
},
}

st.handleTextEditStartEvent(opts)

expect(opts.target.hiddenTextarea.style).toEqual({
width: '10px', height: '10px', fontSize: '10px',
})
})
})
47 changes: 47 additions & 0 deletions packages/drawing/src/textbox-tool.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* globals expect, describe, it, beforeEach, afterEach, jest */
import React from 'react'
import Enzyme from 'enzyme' // eslint-disable-line import/no-extraneous-dependencies
import Adapter from 'enzyme-adapter-react-16' // eslint-disable-line import/no-extraneous-dependencies

import { Drawing, toolEnum } from '../index'

import { TextboxTool } from './tools/textbox'

Enzyme.configure({ adapter: new Adapter() })

const { shallow } = Enzyme

const tokenProvider = () => Promise.resolve('access_token')

describe('TextboxTool tool', () => {
beforeEach(() => {
jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementationOnce(cb => cb())
})

afterEach(() => {})

it('`handleTextEditStartEvent` sets style for hiddenTextarea', () => {
const wrap = shallow((
<Drawing
tokenProvider={tokenProvider}
objects={[]}
tool={toolEnum.TEXT}
/>
))

const instance = wrap.instance()
const tt = new TextboxTool(instance.canvas, undefined, {})

tt.__object = tt.__objectFn()

tt.__object.hiddenTextarea = { style: {} }

tt.handleTextEditStartEvent()

expect(tt.__object.hiddenTextarea.style).toEqual({
width: '10px', height: '10px', fontSize: '10px',
})
})
})
2 changes: 1 addition & 1 deletion packages/drawing/src/tools/lock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */

import { Base } from './base'
import { enhancedFields } from '../drawing'
import { enhancedFields } from '../constants'

export class LockTool extends Base {
static lockObject (object, options = {}) {
Expand Down
8 changes: 8 additions & 0 deletions packages/drawing/src/tools/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ export default class SelectTool extends Base {
}
}

handleTextEditStartEvent (opts) {
if (opts.target && opts.target.hiddenTextarea) {
opts.target.hiddenTextarea.style.width = '10px'
opts.target.hiddenTextarea.style.height = '10px'
opts.target.hiddenTextarea.style.fontSize = '10px'
}
}

handleTextEditEndEvent (opts) {
this._unsetObject()
}
Expand Down
8 changes: 8 additions & 0 deletions packages/drawing/src/tools/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ export class TextboxTool extends PositionableObject {
this._canvas.setActiveObject(this.__object)
this.__object.enterEditing()
}

handleTextEditStartEvent () {
if (this.__object && this.__object.hiddenTextarea) {
this.__object.hiddenTextarea.style.width = '10px'
this.__object.hiddenTextarea.style.height = '10px'
this.__object.hiddenTextarea.style.fontSize = '10px'
}
}
}

0 comments on commit 332466f

Please sign in to comment.