Skip to content

Commit d93bfe5

Browse files
committed
- rename update event to change event.
- check sheet focus before pasting data - support copy tsv format text to clipboard. use `navigator.clipboard`, which doesn't support older browsers.
1 parent ff215bb commit d93bfe5

File tree

12 files changed

+98
-12
lines changed

12 files changed

+98
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Logs
22

3+
## v0.1.0
4+
5+
- rename `update` event to `change` event.
6+
- check sheet focus before pasting data
7+
- support copy tsv format text to clipboard. use `navigator.clipboard`, which doesn't support older browsers.
8+
9+
310
## v0.0.10
411

512
- fix selection issue across frozen cells

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ initialize:
5555
- `render()`: force to re-render visible cells
5656
- `editing(v)`: set edit status to v, or return edit status if v is not provided.
5757

58+
59+
## Event
60+
61+
use `sheet.on('event-name', callback)` to handle sheet related events, e.g.,
62+
63+
s = new sheet( ... );
64+
s.on('change', function({row, col, data, range}) {
65+
....
66+
});
67+
68+
69+
Following are a list of possible events:
70+
71+
- `change`: fired when some cells are updated. callback function with following options, in object:
72+
- `row`: base row of the updated cell
73+
- `col`: base col of the updated cell
74+
- `data`: data used to update. If it's a 2D array and `range` is true, then cells are updated with values in `data` correspondingly relative to `row` and `col` as the original cell
75+
- `range`: true if a range of cells are updated.
76+
77+
5878
## License
5979

6080
MIT

dist/index.js

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "@plotdb/sheet",
44
"license": "MIT",
55
"description": "spreadsheet",
6-
"version": "0.0.10",
6+
"version": "0.1.0",
77
"main": "dist/index.js",
88
"homepage": "https://github.com/plotdb/sheet",
99
"files": [

src/index.ls

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ sheet.prototype = Object.create(Object.prototype) <<< do
9797
@edit {node: p, quick: false}
9898

9999
document.body.addEventListener \paste, (e) ~>
100-
# TODO we need to find some way to ensure sheet gets users' focus before handling paste event
100+
if !parent(document.activeElement, '.sheet', dom) => return
101101
if !@les.start => return
102102
data = e.clipboardData.getData('text')
103103
data = parse-csv data
104104
@set {row: @les.start.row, col: @les.start.col, data: data, range: true}
105105

106106
dom.addEventListener \keydown, (e) ~>
107+
if e.keyCode == 67 and (e.metaKey or e.ctrlKey) => return @copy!
107108
if !@event-in-scope(e) => return
108109
code = e.keyCode
109110
if code == 8 =>
@@ -162,6 +163,16 @@ sheet.prototype = Object.create(Object.prototype) <<< do
162163
e.preventDefault!
163164
), {passive: false}
164165

166+
copy: ->
167+
if !@les.start => return
168+
c = []
169+
for row from @les.start.row til @les.end.row + 1=>
170+
r = []
171+
for col from @les.start.col til @les.end.col + 1 =>
172+
r.push ('"' + (('' + @data[][row][col]) or '').replace(/"/g,'""') + '"')
173+
c.push r.join(\\t)
174+
s = c.join(\\n)
175+
navigator.clipboard.writeText s
165176
166177
event-in-scope: (e) -> (parent e.target, '.sheet', @dom.sheet) == @dom.sheet
167178
regrid: ->
@@ -189,7 +200,7 @@ sheet.prototype = Object.create(Object.prototype) <<< do
189200
for r from 0 til data.length => for c from 0 til data[r].length =>
190201
@data[][r + row][c + col] = data[r][c]
191202
@_content {y: r + row - @pos.row + @xif.row.1, x: c + col - @pos.col + @xif.col.1}
192-
@fire \update, {row, col, data, range: !!range}
203+
@fire \change, {row, col, data, range: !!range}
193204
194205
# re-render cell with the content they suppose to have
195206
_content: ({x, y, n}) ->

web/.view/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pug_mixins["script"]("assets/lib/@plotdb/sheet/dev/index.min.js");
170170
;pug_debug_line = 15;pug_debug_filename = "src\u002Fpug\u002Findex.pug";
171171
pug_html = pug_html + "\u003Cscript\u003E";
172172
;pug_debug_line = 15;pug_debug_filename = "src\u002Fpug\u002Findex.pug";
173-
pug_html = pug_html + "var s, c;\ns = new sheet({\n root: '#root',\n size: {\n col: [48, 64, 92, 128].map(function(it){\n return it + \"px\";\n }),\n row: [48, 64, 92, 128].map(function(it){\n return it + \"px\";\n })\n },\n data: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]],\n idx: {\n row: true,\n col: true\n },\n fixed: {\n row: 2,\n col: 2\n },\n frozen: {\n row: 2,\n col: 2\n }\n});\ns.on('update', function(it){\n return console.log(it);\n});\nc = s.cell({\n x: 1,\n y: 1\n});\nconsole.log(c);\nc.innerHTML = \"\u003Cdiv class=\\\"dropdown\\\"\u003E\\n\u003Cdiv class=\\\"btn btn-sm btn-primary dropdown-toggle\\\" data-toggle=\\\"dropdown\\\"\u003Eclick me\u003C\u002Fdiv\u003E\\n\u003C\u002Fdiv\u003E\";\u003C\u002Fscript\u003E\u003C\u002Fbody\u003E\u003C\u002Fhtml\u003E";
173+
pug_html = pug_html + "var s, c;\ns = new sheet({\n root: '#root',\n size: {\n col: [48, 64, 92, 128].map(function(it){\n return it + \"px\";\n }),\n row: [48, 64, 92, 128].map(function(it){\n return it + \"px\";\n })\n },\n data: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]],\n idx: {\n row: true,\n col: true\n },\n fixed: {\n row: 2,\n col: 2\n },\n frozen: {\n row: 2,\n col: 2\n }\n});\ns.on('change', function(it){\n return console.log(it);\n});\nc = s.cell({\n x: 1,\n y: 1\n});\nconsole.log(c);\nc.innerHTML = \"\u003Cdiv class=\\\"dropdown\\\"\u003E\\n\u003Cdiv class=\\\"btn btn-sm btn-primary dropdown-toggle\\\" data-toggle=\\\"dropdown\\\"\u003Eclick me\u003C\u002Fdiv\u003E\\n\u003C\u002Fdiv\u003E\";\u003C\u002Fscript\u003E\u003C\u002Fbody\u003E\u003C\u002Fhtml\u003E";
174174
}.call(this, "JSON" in locals_for_with ?
175175
locals_for_with.JSON :
176176
typeof JSON !== 'undefined' ? JSON : undefined, "b64img" in locals_for_with ?

web/src/pug/index.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ block script
2727
idx: {row: true, col: true}
2828
fixed: {row: 2, col: 2}
2929
frozen: {row: 2, col: 2}
30-
s.on \update, -> console.log it
30+
s.on \change, -> console.log it
3131
c = s.cell {x: 1, y: 1}
3232
console.log c
3333
c.innerHTML = """

web/static/assets/lib/@plotdb/sheet/dev/index.js

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)