Skip to content

Commit

Permalink
feat(*): dblclick set title
Browse files Browse the repository at this point in the history
  • Loading branch information
ctank committed Aug 14, 2019
1 parent 43395fc commit 37c0b24
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/assets/styles/bpd-core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
@import './features/select';
@import './features/snapline';
@import './features/tooltip';
@import './features/edit-name';
19 changes: 19 additions & 0 deletions src/assets/styles/features/_edit-name.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.shape-name-edit {
position: absolute;
overflow: hidden;
background: #ffffff;
border: 1px solid #000000;
border-radius: 2px;
padding: 0px;
cursor: text;
z-index: 10;
}

.shape-name-temp {
padding: 0px;
border: none;
background: transparent;
opacity: 0;
height: 0px;
overflow: hidden;
}
2 changes: 2 additions & 0 deletions src/core/designer.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class Designer {
for (let key in data) {
switch (key) {
// 不处理
case '$instanceOf':
case '$type':
case 'flowElements':
case 'sourceRef':
Expand Down Expand Up @@ -422,6 +423,7 @@ class Designer {
for (let key in plane) {
switch (key) {
// 不处理
case '$instanceOf':
case '$type':
break
// id与数据id对应
Expand Down
6 changes: 6 additions & 0 deletions src/draw/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ class Draw extends Operation {
this.init()
}
init() {
// 移动
this.$container
.off('mousemove.operate touchstart.operate')
.on('mousemove.operate touchstart.operate', this.move.bind(this))

// 双击
this.$container.off('dblclick.element').on('dblclick.element', () => {
eventBus.trigger('edit.shape.name')
})

// 创建图形
eventBus.on('shape.create', this.createShapeData.bind(this))
// 渲染图形
Expand Down
18 changes: 11 additions & 7 deletions src/draw/drawShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,19 @@ class DrawShape {
const scrollTop = $textBox.scrollTop()

let top = 0
if (fontStyle.vAlign === 'middle') {
top = textBlock.y + textBlock.height / 2 - scrollTop / 2
} else {
if (shape.fontStyle.vAlign === 'bottom') {
let margin = 10
switch (fontStyle.vAlign) {
case 'middle':
top = textBlock.y + textBlock.height / 2 - scrollTop / 2
break
case 'bottom':
top = textBlock.y + textBlock.height - scrollTop
} else {
top = textBlock.y
}
break
default:
top = textBlock.y += margin
break
}

let position = {
x: textBlock.x + textBlock.width / 2,
y: top + scrollTop / 2
Expand Down
4 changes: 2 additions & 2 deletions src/draw/shape/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class Base {
// 字体样式
this.fontStyle = {
fontFamily: 'Arial',
size: 13,
size: 12,
color: '50,50,50',
bold: false,
italic: false,
underline: false,
textAlign: 'center',
vAlign: 'middle',
vAlign: 'top',
orientation: 'vertical'
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/draw/shape/EndEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ class EndEvent extends Shape {
* 获取文本范围
*/
getTextBlock() {
const { textStyle } = this.style
const { height, width } = this.plane.bounds
return { x: width / 2 - 60, y: height, width: 120, height: 30 }
let textWidth = 120
let textHeight = 30
if (textStyle) {
textWidth = textStyle.width || textWidth
textHeight = textStyle.height || textHeight
}
return {
x: width / 2 - textWidth / 2,
y: height,
width: textWidth,
height: textHeight
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/draw/shape/ServiceTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class ServiceTask extends Shape {

this.groupName = 'Task'

// 字体样式
this.fontStyle.vAlign = 'middle'

this.actions = this.getPath()
}
/**
Expand Down
14 changes: 13 additions & 1 deletion src/draw/shape/StartEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ class StartEvent extends Shape {
* 获取文本范围
*/
getTextBlock() {
const { textStyle } = this.style
const { height, width } = this.plane.bounds
return { x: width / 2 - 60, y: height, width: 120, height: 30 }
let textWidth = 120
let textHeight = 30
if (textStyle) {
textWidth = textStyle.width || textWidth
textHeight = textStyle.height || textHeight
}
return {
x: width / 2 - textWidth / 2,
y: height,
width: textWidth,
height: textHeight
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/draw/shape/TerminateEndEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,20 @@ class EndEvent extends Shape {
* 获取文本范围
*/
getTextBlock() {
const { textStyle } = this.style
const { height, width } = this.plane.bounds
return { x: width / 2 - 60, y: height, width: 120, height: 30 }
let textWidth = 120
let textHeight = 30
if (textStyle) {
textWidth = textStyle.width || textWidth
textHeight = textStyle.height || textHeight
}
return {
x: width / 2 - textWidth / 2,
y: height,
width: textWidth,
height: textHeight
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/draw/shape/UserTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class UserTask extends Shape {

this.groupName = 'Task'

// 字体样式
this.fontStyle.vAlign = 'middle'

this.actions = this.getPath()
}
/**
Expand Down Expand Up @@ -85,8 +88,21 @@ class UserTask extends Shape {
* 获取文本范围
*/
getTextBlock() {
const { textStyle } = this.style
const { height, width } = this.plane.bounds
return { x: 10, y: 0, width: width - 20, height: height }

let textWidth = width - 20
let textHeight = height
if (textStyle) {
textWidth = textStyle.width || textWidth
textHeight = textStyle.height || textHeight
}
return {
x: width / 2 - textWidth / 2,
y: height / 2 - textHeight / 2,
width: textWidth,
height: textHeight
}
}
}

Expand Down
168 changes: 168 additions & 0 deletions src/features/edit-name/editName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import eventBus from '../../core/eventBus'
import $ from '../../utils/slimJQ'
import { setScale, restoreScale } from '../../utils/utils'

const DEFAULT_CONFIG = {
// 边框颜色
borderColor: '#FF884D',
// 修改名称回调
onEdited: () => {}
}

class EditName {
constructor($container, config) {
this.$container = $container

this.config = Object.assign({}, DEFAULT_CONFIG, config)

this.init()
}

init() {
//
eventBus.on('edit.shape.name', this.editShapeName.bind(this))
}

editShapeName() {
const { $container, config } = this

const selectIds = eventBus.trigger('shape.select.getIds') || []
if (selectIds.length === 1) {
const element = eventBus.trigger('element.get', selectIds[0])
console.log(element)

const { data, plane, shape } = element

if (shape.bpmnName === 'SequenceFlow') {
this.editConnectionName(element)
return true
}

let $edit = $container.find('.shape-name-edit')
if ($edit.length === 0) {
$edit = $("<textarea class='shape-name-edit'></textarea>").appendTo(
$container.find('.bpd-designer')
)
}

let $temp = $container.find('.shape-name-temp')
if ($temp.length === 0) {
$temp = $("<textarea class='shape-name-temp'></textarea>").appendTo(
$container.find('.bpd-designer')
)
}

$('.text-box[data-shape=' + data.id + ']').hide()

const fontStyle = shape.fontStyle
const textBlock = shape.getTextBlock()
const editStyle = {
width: textBlock.width + 'px',
'border-color': config.borderColor,
'line-height': Math.round(fontStyle.size * 1.25) + 'px',
'font-size': fontStyle.size + 'px',
'font-family': fontStyle.fontFamily,
'font-weight': fontStyle.bold ? 'bold' : 'normal',
'font-style': fontStyle.italic ? 'italic' : 'normal',
'text-align': fontStyle.textAlign,
color: 'rgb(' + fontStyle.color + ')',
'text-decoration': fontStyle.underline ? 'underline' : 'none'
}

$edit.css(editStyle).show()
$temp.css(editStyle)
textBlock.x += plane.bounds.x
textBlock.y += plane.bounds.y
$edit.val(data.name)

$edit
.off()
.on('keyup', () => {
$temp.val($edit.val())
$temp.scrollTop(99999)
const tempHeight = $temp.scrollTop()
$edit.css({ height: tempHeight })

const shapeTextPos = {
x: textBlock.x + textBlock.width / 2,
y: textBlock.y + textBlock.height / 2
}
let editPosY = 0
let editPadding = 5
let editPaddingTop = 0
let textHeight = textBlock.height

switch (fontStyle.vAlign) {
case 'middle':
if (tempHeight > textHeight) {
textHeight = tempHeight
editPosY = shapeTextPos.y - textHeight / 2 - editPadding
editPaddingTop = 0
} else {
editPosY = shapeTextPos.y - textBlock.height / 2 - editPadding
editPaddingTop = (textBlock.height - tempHeight) / 2
textHeight = textBlock.height - editPaddingTop
}
break
default:
editPosY = shapeTextPos.y - textBlock.height / 2 + editPadding
if (tempHeight > textHeight) {
textHeight = tempHeight
} else {
textHeight = textBlock.height
}
break
}
const editHeight = editPadding * 2 + textHeight
const editPos = {
x: textBlock.x + textBlock.width / 2 - editPadding,
y: editPosY + editHeight / 2
}
$edit.css({
width: textBlock.width,
height: textHeight,
'padding-top': editPaddingTop,
padding: editPadding,
left: setScale(editPos.x) - textBlock.width / 2 - 1,
top: setScale(editPos.y) - editHeight / 2 - 1
})
})
.on('blur', e => {
this.updateShapeName(element)
})
.on('mousemove', e => {
e.stopPropagation()
})
.on('mousedown', e => {
e.stopPropagation()
})
.on('mouseenter', e => {
console.log('mouseenter')
})

$edit.trigger('keyup')
}
}

editConnectionName(element) {}

updateShapeName(element) {
const { data } = element
const $edit = this.$container.find('.shape-name-edit')
const shapeName = $edit.val()
if ($edit.length && $edit.is(':visible')) {
if (shapeName !== data.name) {
data.name = shapeName
// Model.update(d)
}
// 渲染图形
eventBus.trigger('shape.render', {
type: element.shape.bpmnName,
element
})
$edit.remove()
}
}
}

export default EditName
3 changes: 3 additions & 0 deletions src/features/edit-name/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import editName from './editName'

export default editName
1 change: 0 additions & 1 deletion src/features/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
setScale,
restoreScale,
mergeArray,
cloneDeep,
setExportData
} from '../../utils/utils'
import DrawUtils from '../../draw/drawUtils'
Expand Down
Loading

0 comments on commit 37c0b24

Please sign in to comment.