22 <ul class =" qr-code-list" >
33 <li v-for =" code in allCodes" :key =" code.id" :id =" code.id" class =" qr-code-list__item" :class =" code.selected ? 'qr-code-list__item_selected' : ''" @click =" handleItemClick(code)" >
44 <a class =" qr-code-list__anchor" href =" javascript: void(0);" tabindex =" -1" >
5- <p class =" qr-code-list__title" :title =" code.title" >{{ code.title }}</p >
5+ <p class =" qr-code-list__title" :title =" code.title" @dblclick = " handleTitleDoubleClick " @blur = " handleTitleBlur(arguments[0], code) " @keydown = " handleTitleKeyDown " >{{ code.title }}</p >
66 <p class =" qr-code-list__content" :title =" code.content" >{{ code.content }}</p >
77 </a >
88 </li >
1111
1212<script >
1313import { mapGetters } from ' vuex' ;
14+ import * as cfg from ' @/configs' ;
1415
1516export default {
1617 computed: {
@@ -23,6 +24,58 @@ export default {
2324 handleItemClick (code ) {
2425 this .$store .dispatch (' selectCode' , code .id );
2526 },
27+ handleTitleDoubleClick (ev ) {
28+ const body = document .querySelector (' body' );
29+ const element = ev .currentTarget ;
30+
31+ element .contentEditable = true ;
32+ element .parentElement .parentElement .classList .add (' qr-code-list__item_edit' );
33+ if (body .createTextRange ) {
34+ const range = body .createTextRange ();
35+
36+ range .moveToElementText (element);
37+ range .select ();
38+ } else if (window .getSelection ) {
39+ const selection = window .getSelection ();
40+ const range = document .createRange ();
41+
42+ range .selectNodeContents (element);
43+ selection .removeAllRanges ();
44+ selection .addRange (range);
45+ }
46+ },
47+ handleTitleKeyDown (ev ) {
48+ if (ev .keyCode === cfg .KEY_ENTER ) {
49+ ev .preventDefault ();
50+ ev .currentTarget .blur ();
51+ }
52+ },
53+ handleTitleBlur (ev , code ) {
54+ const element = ev .currentTarget ;
55+ const title = element .textContent .replace (/ \n | \r / g , ' ' );
56+
57+ element .contentEditable = false ;
58+ element .innerHTML = title;
59+ element .parentElement .parentElement .classList .remove (' qr-code-list__item_edit' );
60+ if (! title) {
61+ element .innerHTML = code .title ;
62+ return ;
63+ }
64+ if (title === code .title ) {
65+ return ;
66+ }
67+ if (this .allCodes .some (c => c .title === title)) {
68+ this .$store .dispatch (' showToast' , ' Duplicate record title' );
69+ element .innerHTML = code .title ;
70+ return ;
71+ }
72+ if (title !== code .title ) {
73+ this .$store .dispatch (' updateCode' , {
74+ id: code .id ,
75+ title,
76+ });
77+ }
78+ },
2679 },
2780};
2881 </script >
@@ -40,22 +93,6 @@ export default {
4093 position : relative ;
4194 }
4295
43- & __item :active ,
44- & __item_selected {
45- background-color : #efefef ;
46- border-bottom-color : #e0e5e6 ;
47-
48- & :after {
49- content : ' ' ;
50- width : 100% ;
51- height : 1px ;
52- background-color : #e0e5e6 ;
53- top : -1px ;
54- left : 0 ;
55- position : absolute ;
56- }
57- }
58-
5996 & __anchor {
6097 color : inherit ;
6198 text-decoration : none ;
@@ -68,18 +105,56 @@ export default {
68105 margin-bottom : 0 ;
69106 width : 100% ;
70107 overflow : hidden ;
71- text-overflow : ellipsis ;
72108 white-space : nowrap ;
109+ position : relative ;
110+ }
111+
112+ & __title ::after ,
113+ & __content ::after {
114+ content : ' ' ;
115+ width : 20px ;
116+ top : 0 ;
117+ right : 0 ;
118+ bottom : 0 ;
119+ position : absolute ;
120+ background-image : linear-gradient (to left , #f6f8f8 20% , transparent );
73121 }
74122
75123 & __title {
124+ outline : none ;
76125 margin-bottom : 5px ;
77126 }
78127
79128 & __content {
80129 color : #cccccc ;
81130 font-size : 14px ;
82131 }
132+
133+ // state
134+ // ====
135+ & __item_selected {
136+ background-color : #efefef ;
137+ border-bottom-color : #e0e5e6 ;
138+ }
139+
140+ & __item_selected ::after {
141+ content : ' ' ;
142+ width : 100% ;
143+ height : 1px ;
144+ background-color : #e0e5e6 ;
145+ top : -1px ;
146+ left : 0 ;
147+ position : absolute ;
148+ }
149+
150+ & __item_selected & __title ::after ,
151+ & __item_selected & __content ::after {
152+ background-image : linear-gradient (to left , #efefef 20% , transparent );
153+ }
154+
155+ & __item_edit & __title ::after {
156+ background-image : none ;
157+ }
83158 }
84159 </style >
85160
0 commit comments