Skip to content

Commit 9d168b4

Browse files
committed
fix bookmarklet
1 parent 354e22a commit 9d168b4

File tree

3 files changed

+45
-86
lines changed

3 files changed

+45
-86
lines changed

bookmarks-ts/bookmarklet/App.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,3 @@ export default {
1818
}
1919
}
2020
</script>
21-
<style lang="scss">
22-
@import './app.scss';
23-
</style>
24-

bookmarks-ts/bookmarklet/main.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
declare function require(path: string): any;
22

3-
function resolveRpcHost(host: string) {
4-
let colon = host.lastIndexOf(':'),
5-
rpc_port = parseInt(host.substring(colon + 1), 10) - 1
6-
7-
return 'http://' + host.substring(0, colon + 1) + rpc_port
3+
function isLocal(host: string) {
4+
return host === '127.0.0.1' || host === 'localhost'
85
}
96

107
let rpc_host = window['rpc_host']
11-
if (!rpc_host)
12-
window['rpc_host'] = resolveRpcHost(window.location.host)
8+
if (!rpc_host && '8080' === window.location.port && isLocal(window.location.hostname))
9+
window['rpc_host'] = 'http://127.0.0.1:5010'
1310

1411
import * as Vue from 'vue'
1512

bookmarks-ts/bookmarklet/user/index.ts

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { component } from 'vuets'
2-
import { defp, extractMsg, bit_clear_and_set, base64ToBytes, to_int32LE } from 'coreds/lib/util'
2+
import { defp, extractMsg, bit_clear_and_set } from 'coreds/lib/util'
33
import { PojoState, HasState } from 'coreds/lib/types'
44
import { diffFieldTo } from 'coreds/lib/diff'
55
import { bindFocus, debounce, Keys } from 'coreds-ui/lib/dom_util'
6-
import * as Vue from 'vue'
6+
import { nextTick } from 'vue'
77
import { user } from '../../g/user/'
88
const $ = user.BookmarkEntry,
99
M = user.BookmarkEntry.M
10-
const nextTick = Vue.nextTick
1110

1211
interface Config {
1312
url: string
@@ -23,37 +22,12 @@ function $validateNotes() {}*/
2322
const MAX_TAGS = 4,
2423
SUGGEST_TAGS_LIMIT = 12
2524

26-
interface Tag {
27-
id: number
28-
name : string
29-
state: number
30-
}
31-
32-
const enum TagState {
33-
CURRENT = 32
34-
}
35-
36-
export function toTagArray(serTags: string, names: string[]): Tag[] {
37-
let list: Tag[] = [],
38-
bytes = base64ToBytes(serTags)
39-
40-
for (let i = 0, j = 0, len = bytes.length; i < len; i += 4) {
41-
let id = to_int32LE(bytes, i),
42-
name = names[j++]/*,
43-
color = tag && tag[Tag0.color] ? ('#' + tag[Tag0.color]) : DEFAULT_TAG_COLOR*/
44-
45-
list.push({ id, name, state: 0 })
46-
}
47-
48-
return list
49-
}
50-
5125
interface Entry extends HasState {
5226
title: string
5327
notes: string
54-
tags: Tag[]
28+
tags: user.BookmarkTag.M[]
5529
// suggest fields
56-
suggest_tags: Tag[]
30+
suggest_tags: user.BookmarkTag.M[]
5731
tag_idx: number
5832
tag_name: string
5933
}
@@ -62,21 +36,13 @@ interface M {
6236
original: Entry
6337
}
6438

65-
function mapTag(item): Tag {
66-
return {
67-
id: item['3'],
68-
name: item['1'],
69-
state: 0
70-
}
71-
}
72-
73-
function mapId(item: Tag): number {
74-
return item.id
39+
function mapId(item: user.BookmarkTag.M): number {
40+
return item[user.BookmarkTag.M.$.id]
7541
}
7642

7743
function handleKeyEvent(e: KeyboardEvent, pojo: Entry, self: Home, fn_name: string): boolean {
78-
let suggest_tags: Tag[],
79-
tag: Tag,
44+
let suggest_tags: user.BookmarkTag.M[],
45+
tag: user.BookmarkTag.M,
8046
idx: number
8147
switch (e.which) {
8248
case Keys.ESCAPE:
@@ -90,12 +56,10 @@ function handleKeyEvent(e: KeyboardEvent, pojo: Entry, self: Home, fn_name: stri
9056

9157
if (idx !== 0) {
9258
tag = suggest_tags[idx - 1]
93-
tag.state ^= TagState.CURRENT
9459
}
9560

9661
tag = suggest_tags[idx]
97-
tag.state |= TagState.CURRENT
98-
62+
// selected idx
9963
pojo.tag_idx = idx
10064
break
10165
case Keys.UP:
@@ -108,17 +72,13 @@ function handleKeyEvent(e: KeyboardEvent, pojo: Entry, self: Home, fn_name: stri
10872
// select last
10973
idx = suggest_tags.length - 1
11074
tag = suggest_tags[idx]
111-
tag.state |= TagState.CURRENT
75+
// selected idx
11276
pojo.tag_idx = idx
11377
break
11478
}
115-
116-
tag = suggest_tags[idx--]
117-
tag.state ^= TagState.CURRENT
118-
119-
tag = suggest_tags[idx]
120-
tag.state |= TagState.CURRENT
121-
79+
80+
tag = suggest_tags[--idx]
81+
// selected idx
12282
pojo.tag_idx = idx
12383
break
12484
case Keys.ENTER:
@@ -176,7 +136,7 @@ export class Home {
176136
}
177137
pnew_tag$$F: any
178138
pnew_tag$$focus: any
179-
139+
180140
m: M
181141

182142
static created(self: Home) {
@@ -245,7 +205,7 @@ export class Home {
245205

246206
let original = array[0],
247207
pupdate = this.pupdate,
248-
tags = toTagArray(original[$.$.serTags], original[M.$.tags]),
208+
tags = original[M.$.tags],
249209
tagCount = tags.length
250210

251211
this.m.original = original
@@ -302,9 +262,9 @@ export class Home {
302262

303263
this.prepare(pojo)
304264

305-
user.BookmarkTag.$NAME({"1": val, "4": {"1": false, "2": SUGGEST_TAGS_LIMIT}}).then((data) => {
265+
user.BookmarkTag.$NAME({"1": val, "4": {"1": false, "2": SUGGEST_TAGS_LIMIT}}, true).then((data) => {
306266
let array = data['1'] as any[]
307-
pojo.suggest_tags = array && array.length ? array.map(mapTag) : []
267+
pojo.suggest_tags = array || []
308268
pojo.tag_idx = -1
309269

310270
this.success(pojo)
@@ -329,8 +289,8 @@ export class Home {
329289
window.close()
330290
}).then(undefined, this.pnew$$F)
331291
}
332-
addTag(tag: Tag) {
333-
let id = tag.id,
292+
addTag(tag: user.BookmarkTag.M) {
293+
let id = mapId(tag),
334294
pnew = this.pnew,
335295
tags = pnew.tags,
336296
i = 0,
@@ -340,7 +300,7 @@ export class Home {
340300
pnew.tag_name = ''
341301

342302
for (; i < len; i++) {
343-
if (id === tags[i].id) {
303+
if (id === mapId(tags[i])) {
344304
// dup
345305
nextTick(this.pnew$$focus_tag)
346306
return
@@ -351,15 +311,15 @@ export class Home {
351311
if (tags.length < MAX_TAGS)
352312
nextTick(this.pnew$$focus_tag)
353313
}
354-
rmTag(tag: Tag, update?: boolean) {
355-
let id = tag.id,
314+
rmTag(tag: user.BookmarkTag.M, update?: boolean) {
315+
let id = mapId(tag),
356316
pojo = update ? this.pupdate : this.pnew,
357317
tags = pojo.tags,
358318
i = 0,
359319
len = tags.length
360320

361321
for (; i < len; i++) {
362-
if (id === tags[i].id) {
322+
if (id === mapId(tags[i])) {
363323
if (!update)
364324
tags.splice(i, 1)
365325
break
@@ -388,8 +348,8 @@ export class Home {
388348

389349
nextTick(this.pupdate$$focus_tag)
390350
}
391-
insertTag(tag: Tag) {
392-
let id = tag.id,
351+
insertTag(tag: user.BookmarkTag.M) {
352+
let id = mapId(tag),
393353
pupdate = this.pupdate,
394354
tags = pupdate.tags,
395355
i = 0,
@@ -399,7 +359,7 @@ export class Home {
399359
pupdate.tag_name = ''
400360

401361
for (; i < len; i++) {
402-
if (id === tags[i].id) {
362+
if (id === mapId(tags[i])) {
403363
// dup
404364
nextTick(this.pupdate$$focus_tag)
405365
return
@@ -466,8 +426,8 @@ export default component({
466426
</div>
467427
<div class="dropdown" :class="{ active: pnew.suggest_tags.length }">
468428
<ul class="dropdown-menu mfluid">
469-
<li v-for="tag in pnew.suggest_tags" :class="{ current: !!(tag.state & ${TagState.CURRENT}) }">
470-
<a @click.prevent="addTag(tag)">{{tag.name}}</a>
429+
<li v-for="(tag, idx) of pnew.suggest_tags" :class="{ current: idx === pnew.tag_idx }">
430+
<a @click.prevent="addTag(tag)">{{ tag['${user.BookmarkTag.M.$.name}'] }}</a>
471431
</li>
472432
</ul>
473433
</div>
@@ -476,8 +436,11 @@ export default component({
476436
:disabled="!!(pnew.state & ${PojoState.LOADING}) || (!!pnew.tag_name && !pnew.tags.length)"><b>Submit</b></button>
477437
</div>
478438
<ul class="tags">
479-
<li v-for="tag in pnew.tags">
480-
<a>{{tag.name}}<button class="b" @click.prevent="rmTag(tag, false)">x</button></a>
439+
<li v-for="tag of pnew.tags">
440+
<a>
441+
{{ tag['${user.BookmarkTag.M.$.name}'] }}
442+
<button class="b" @click.prevent="rmTag(tag, false)">x</button>
443+
</a>
481444
</li>
482445
</ul>
483446
</div>
@@ -499,15 +462,18 @@ export default component({
499462
</div>
500463
<div class="dropdown" :class="{ active: pupdate.suggest_tags.length }">
501464
<ul class="dropdown-menu mfluid">
502-
<li v-for="tag in pupdate.suggest_tags" :class="{ current: !!(tag.state & ${TagState.CURRENT}) }">
503-
<a @click.prevent="insertTag(tag)">{{tag.name}}</a>
465+
<li v-for="(tag, idx) of pupdate.suggest_tags" :class="{ current: idx === pupdate.tag_idx }">
466+
<a @click.prevent="insertTag(tag)">{{ tag['${user.BookmarkTag.M.$.name}'] }}</a>
504467
</li>
505468
</ul>
506469
</div>
507470
<ul class="tags">
508-
<li v-for="tag in pupdate.tags">
509-
<a>{{tag.name}}<button class="b" @click.prevent="rmTag(tag, true)"
510-
:disabled="!!(pupdate.state & ${PojoState.LOADING})">x</button></a>
471+
<li v-for="tag of pupdate.tags">
472+
<a>
473+
{{ tag['${user.BookmarkTag.M.$.name}'] }}
474+
<button class="b" @click.prevent="rmTag(tag, true)"
475+
:disabled="!!(pupdate.state & ${PojoState.LOADING})">x</button>
476+
</a>
511477
</li>
512478
</ul>
513479
</div>

0 commit comments

Comments
 (0)