Skip to content

Commit 1fa9620

Browse files
committedDec 3, 2017
完成删除节点的操作
1 parent 45a2eca commit 1fa9620

File tree

6 files changed

+195
-10
lines changed

6 files changed

+195
-10
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
## TODO
10-
- 节点为文字为空时再按删除键,删除当前节点
10+
- ~~节点为文字为空时再按删除键,删除当前节点~~
1111
- 序列化和反序列化
1212
- 使用 redux 随时保存文档状态到 LocalStroage
1313
- 同步文档到服务器(需要后台 API)

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"babel-plugin-import": "^1.6.2",
1515
"babel-runtime": "^6.9.2",
1616
"dva": "^1.2.1",
17+
"jquery": "^3.2.1",
1718
"lodash": "^4.17.4",
1819
"react": "^15.4.0",
1920
"react-dom": "^15.4.0"

‎public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<title>Dva Demo</title>
6+
<title>like Workflowy</title>
77
<link rel="stylesheet" href="/index.css" />
88
</head>
99
<body>

‎src/components/AppView.js

+135-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {Component} from 'react';
22
import _ from 'lodash';
33
import {Affix, Button} from 'antd';
44
import NodeView from './NodeView';
5+
import { relativeTimeRounding } from 'moment';
56
class AppView extends Component {
67
constructor(props) {
78
super(props);
@@ -474,7 +475,7 @@ class AppView extends Component {
474475

475476
}
476477
onFocusChanged = (id, isFocus)=> {
477-
// console.log('onFocusChanged',id, isFocus)
478+
console.log('onFocusChanged',id, isFocus)
478479
let {focusId} = this.state
479480
if (isFocus) focusId = id
480481
else focusId = null
@@ -517,8 +518,12 @@ class AppView extends Component {
517518

518519

519520

520-
let rootNode = {id: 'root'}
521+
let rootNode = {id: 'root'}
522+
console.log('begin generateStateData' ,rootNode,nodes, _.cloneDeep(releations))
523+
521524
this.generateStateData(rootNode, nodes, releations)
525+
console.log('after generateStateData' ,rootNode,nodes, _.cloneDeep(releations))
526+
522527
this.setState({
523528
data: rootNode,
524529
nodes,
@@ -529,10 +534,137 @@ class AppView extends Component {
529534
this.printReleations(_.cloneDeep(releations))
530535

531536
}
537+
onDelete = (id) => {
538+
console.log('onDelete', id);
539+
let {data, nodes, releations} = this.state
540+
console.log('begin----------------------onDelete------------------------------------begin')
541+
this.printReleations(_.cloneDeep(releations))
542+
console.log('begin onDelete' , _.cloneDeep(releations))
543+
// console.log('clone releations', _.cloneDeep(releations))
544+
545+
console.log('onDelete', id)
546+
let rootNode = {id: 'root'}
547+
// _.find(releations, d => d.id == id).text = text
548+
let node = _.find(releations, d => d.id == id)
549+
let parentNode =null
550+
if(node) parentNode = _.find(releations, d => d.id == node.parent_id)
551+
let leftNode =null
552+
if(node) leftNode = _.find(releations, d => d.id == node.left_id)
553+
let rightNode =null
554+
if(node) rightNode = _.find(releations, d => d.id == node.right_id)
555+
let parentParentNode = null
556+
if (parentNode) parentParentNode = _.find(releations, d => d.id == parentNode.parent_id)
557+
558+
559+
console.log('curentNode', node)
560+
console.log('parentNode', parentNode)
561+
console.log('leftNode', leftNode)
562+
console.log('rightNode', rightNode)
563+
console.log('parentParentNode', parentParentNode)
564+
// if (n)
565+
if (parentNode.id == 'root' && !leftNode) {
566+
console.log('only one node , skip delete ')
567+
return
568+
569+
}
570+
let children = _.find(releations, d => d.parent_id == id)
571+
if (_.isEmpty(children)) {
572+
// DO Delete
573+
console.log(' DO Delete ')
574+
// node.parent_id = parentNode.id
575+
576+
if (leftNode && rightNode){
577+
leftNode.right_id = rightNode.id
578+
rightNode.left_id = leftNode.id
579+
} else {
580+
if (leftNode && !rightNode){
581+
leftNode.right_id = null
582+
}
583+
if (rightNode && !leftNode){
584+
rightNode.left_id = null
585+
}
586+
587+
}
588+
let new_releation = null;
589+
if (leftNode){
590+
new_releation = leftNode
591+
} else {
592+
if (parentNode) new_releation = parentNode
593+
}
594+
let focusId = new_releation ? new_releation.id : null
595+
this.check(_.clone(releations))
596+
597+
this.setState({focusId}, ()=> {
598+
releations = _.filter(releations, d => d.id != id)
599+
nodes = _.filter(nodes, d => d.id != id)
600+
// this.setState({releations, nodes})
601+
// let rootNode = {id: 'root'}
602+
this.generateStateData(rootNode, nodes, releations)
603+
this.setState({
604+
data: rootNode,
605+
nodes,
606+
releations,
607+
focusId
608+
}, () => this.save())
609+
this.domFocus(focusId);
610+
console.log('end onDelete setState' ,rootNode,nodes, _.cloneDeep(releations), focusId)
611+
this.printReleations(_.cloneDeep(releations))
612+
})
613+
614+
615+
616+
}
617+
618+
}
619+
onDirectionChange = (id, direction)=> {
620+
console.log('onDirectionChange' , id, direction);
621+
let {data, nodes, releations} = this.state
622+
// console.log('begin----------------------onDirectionChange------------------------------------begin')
623+
// this.printReleations(_.cloneDeep(releations))
624+
// console.log('begin onDirectionChange' , _.cloneDeep(releations))
625+
// console.log('clone releations', _.cloneDeep(releations))
626+
627+
// console.log('onDirectionChange', id)
628+
let rootNode = {id: 'root'}
629+
// _.find(releations, d => d.id == id).text = text
630+
let node = _.find(releations, d => d.id == id)
631+
let parentNode =null
632+
if(node) parentNode = _.find(releations, d => d.id == node.parent_id)
633+
let leftNode =null
634+
if(node) leftNode = _.find(releations, d => d.id == node.left_id)
635+
let rightNode =null
636+
if(node) rightNode = _.find(releations, d => d.id == node.right_id)
637+
let parentParentNode = null
638+
if (parentNode) parentParentNode = _.find(releations, d => d.id == parentNode.parent_id)
639+
640+
let focusId = null
641+
if (direction == 'up'){
642+
if (leftNode) focusId = leftNode.id
643+
if (!leftNode && parentNode) focusId = parentNode.id
644+
if(focusId) this.setState({focusId}, ()=>{this.domFocus(focusId)})
645+
}
646+
if (direction == 'down'){
647+
focusId = null
648+
if (rightNode) focusId = rightNode.id
649+
if(focusId) this.setState({focusId}, ()=>{this.domFocus(focusId)})
650+
}
651+
652+
653+
}
654+
domFocus = (id) => {
655+
let input_id = `input_of_${id}`;
656+
let element = document.getElementById(input_id);
657+
console.log('domFocus', id, input_id, element)
658+
if (!id) return;
659+
// if(!ele)
660+
setTimeout(()=>{
661+
if(element) element.focus();
662+
}, 0)
663+
}
532664
render() {
533665
return (
534666
<div>
535-
<NodeView root={true} children={this.state.data.children} onTabChange={this.onTabChange} onTextChange={this.onTextChange} focusId={this.state.focusId} onFocusChanged={this.onFocusChanged} onPressEnter={this.onPressEnter}/>
667+
<NodeView root={true} children={this.state.data.children} onTabChange={this.onTabChange} onTextChange={this.onTextChange} focusId={this.state.focusId} onFocusChanged={this.onFocusChanged} onPressEnter={this.onPressEnter} onDelete={this.onDelete} onDirectionChange={this.onDirectionChange}/>
536668
</div>
537669
)
538670
}

‎src/components/NodeView.js

+53-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
import React, {Component} from 'react';
2+
import ReactDOM from 'react-dom';
23
import _ from 'lodash';
34
import {Affix, Button, Badge, Input} from 'antd';
4-
5+
// import jquery from 'jquery';
6+
// window.$ = jquery
57
class NodeView extends Component {
68
constructor(props) {
79
super(props);
10+
811
// console.log('NodeView props', props)
912

1013
}
14+
componentWillReceiveProps(props) {
15+
if (props){
16+
this.props = props;
17+
const {focusId, id} = props
18+
if (id && focusId &&id == focusId){
19+
// console.log('will focus self:', id, focusId, this);
20+
// let input_id = `input_of_${id}`;
21+
// let element = document.getElementById(input_id);
22+
// console.log('getElementById', input_id, element, jquery(`${input_id}`));
23+
// jquery(`${input_id}`).focus();
24+
// setTimeout(()=>{
25+
// element.focus();
26+
// }, 200)
27+
// jquery(input_id);
28+
29+
// element.focus();
30+
// ReactDOM.findDOMNode(input_id).focus();
31+
32+
// this.refs.input.auto
33+
// this.refs.input.refs.input.focus(true);
34+
// this.refs.input.focus(true);
35+
36+
37+
}
38+
39+
}
40+
}
1141
onChange = (e) => {
1242
const {root, children,onTextChange, id} = this.props
1343
const text = e.target.value
@@ -18,7 +48,7 @@ class NodeView extends Component {
1848
}
1949
}
2050
onKeyDown = (e)=> {
21-
const {root, children,onTextChange,onTabChange, id} = this.props
51+
const {root, children,onTextChange,onTabChange, id, onDelete, onDirectionChange} = this.props
2252
const text = e.target.text
2353
const target = e.target
2454
const {key, keyCode, shiftKey,ctrlKey, altKey} = e
@@ -36,7 +66,23 @@ class NodeView extends Component {
3666
onTabChange(id, false)
3767
e.preventDefault();
3868
}
39-
}
69+
}
70+
if (keyCode == 8 && _.isEmpty(this.props.text)){
71+
// console.log("Backspace clicked");
72+
if (onDelete) {
73+
onDelete(id)
74+
e.preventDefault();
75+
}
76+
}
77+
if (keyCode >= 37 && keyCode <= 40 && onDirectionChange ) {
78+
const temp ={
79+
'37': 'left',
80+
'38': 'up',
81+
'39': 'right',
82+
'40': 'down'
83+
}
84+
onDirectionChange(id, temp[keyCode.toString()])
85+
}
4086
}
4187
onBlur = (e)=>{
4288
// console.log('onBlur',e)
@@ -91,6 +137,8 @@ class NodeView extends Component {
91137
borderRadius: 3,}}></div></div>
92138
{/* <Badge status="default"/> */}
93139
<Input
140+
id={`input_of_${id}`}
141+
ref='input'
94142
style={{
95143
borderWidth: 0,
96144
}}
@@ -109,7 +157,7 @@ class NodeView extends Component {
109157
)
110158
}
111159
render() {
112-
const {root, text, children, onTextChange, onTabChange,focusId,onFocusChanged,onPressEnter, id} = this.props
160+
const {root, text, children, onTextChange, onTabChange,focusId,onFocusChanged, onDirectionChange,onPressEnter, id, onDelete} = this.props
113161
const borderLeft = root ? '0px solid #e5e5e5' : '1px solid #e5e5e5'
114162
const borderColor = focusId == id ? '#c8c8c8' : '#e5e5e5'
115163

@@ -135,7 +183,7 @@ class NodeView extends Component {
135183
{_.map(children, (node) => {
136184
return (
137185
<div key={node.id}>
138-
<NodeView id={node.id} text={node.text} children={node.children} onTextChange={onTextChange} onTabChange={onTabChange} focusId={focusId} onFocusChanged={onFocusChanged} onPressEnter={onPressEnter}/>
186+
<NodeView id={node.id} text={node.text} children={node.children} onTextChange={onTextChange} onTabChange={onTabChange} focusId={focusId} onFocusChanged={onFocusChanged} onPressEnter={onPressEnter} onDelete={onDelete} onDirectionChange={onDirectionChange}/>
139187
</div>
140188
)
141189
})

‎yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,10 @@ istanbul@^0.4.5:
30983098
which "^1.1.1"
30993099
wordwrap "^1.0.0"
31003100

3101+
jquery@^3.2.1:
3102+
version "3.2.1"
3103+
resolved "http://registry.npm.taobao.org/jquery/download/jquery-3.2.1.tgz#5c4d9de652af6cd0a770154a631bba12b015c787"
3104+
31013105
js-base64@^2.1.9:
31023106
version "2.3.2"
31033107
resolved "http://registry.npm.taobao.org/js-base64/download/js-base64-2.3.2.tgz#a79a923666372b580f8e27f51845c6f7e8fbfbaf"

0 commit comments

Comments
 (0)
Please sign in to comment.