Skip to content

Commit 7d22b35

Browse files
committed
- make it possible to add node instead of pure text.
1 parent cfe714d commit 7d22b35

File tree

12 files changed

+97
-29
lines changed

12 files changed

+97
-29
lines changed

CHANGELOG.md

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

3+
## v0.1.5
4+
5+
- make it possible to add node instead of pure text.
6+
7+
38
## v0.1.4
49

510
- fix bug: `0` wasn't shown when set cell content. fix this by checking `!(textContent?)` instead of `!(textContent)`.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ Following are a list of possible events:
7979
- `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
8080
- `range`: true if a range of cells are updated.
8181

82+
## Rich Text
83+
84+
You can add more than texts in the grid of the sheet, by having an object with following structure in data:
85+
86+
{
87+
type: "data-type",
88+
... /* members according to type */
89+
}
90+
91+
Currently, only `node` type is supported:
92+
93+
{ type: "node", node: SomeElement }
94+
8295

8396
## License
8497

dist/index.js

Lines changed: 19 additions & 8 deletions
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.1.4",
6+
"version": "0.1.5",
77
"main": "dist/index.js",
88
"homepage": "https://github.com/plotdb/sheet",
99
"files": [

src/index.ls

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ sheet.prototype = Object.create(Object.prototype) <<< do
196196
for row from @les.start.row til @les.end.row + 1=>
197197
r = []
198198
for col from @les.start.col til @les.end.col + 1 =>
199-
r.push ('"' + (('' + @_data[][row][col]) or '').replace(/"/g,'""') + '"')
199+
content = @_data[][rpw][col]
200+
# TODO advanced content support
201+
if typeof(content) == \object => continue
202+
203+
r.push ('"' + (('' + content) or '').replace(/"/g,'""') + '"')
200204
c.push r.join(\\t)
201205
s = c.join(\\n)
202206
navigator.clipboard.writeText s
@@ -232,7 +236,7 @@ sheet.prototype = Object.create(Object.prototype) <<< do
232236
# re-render cell with the content they suppose to have
233237
_content: ({x, y, n}) ->
234238
if !n and !(n = @dom.inner.childNodes[x + y * @dim.col]) => return
235-
[textContent, className] = if x < @xif.col.0 and y < @xif.col.0 => ["","cell idx"]
239+
[content, className] = if x < @xif.col.0 and y < @xif.col.0 => ["","cell idx"]
236240
else if x < @xif.col.0 =>
237241
v = if y < @xif.row.1 => " "
238242
else if y < @xif.row.2 => y - @xif.row.1 + 1
@@ -252,10 +256,16 @@ sheet.prototype = Object.create(Object.prototype) <<< do
252256
else if y < @xif.row.2 =>
253257
[@_data[][y - @xif.row.1][@pos.col + x - @xif.col.1], "cell frozen"]
254258
else [@_data[][@pos.row + y - @xif.row.1][@pos.col + x - @xif.col.1], "cell"]
255-
if !(textContent?) => textContent = ""
259+
if !(content?) => content = ""
256260
257261
n.className = className
258-
if textContent != null => n.textContent = textContent
262+
if content != null =>
263+
# TODO support advanced content
264+
if typeof(content) == \object =>
265+
if content.type == \dom =>
266+
n.textContent = ""
267+
n.appendChild content.node
268+
else n.textContent = content
259269
260270
# move down
261271
_md: (mag = 1) ->

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('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";
173+
pug_html = pug_html + "var node, div, s, c;\nnode = {\n type: 'dom',\n node: div = document.createElement('div')\n};\ndiv.setAttribute('class', \"d-flex w-100 justify-content-between h-100 align-items-center\");\ndiv.innerHTML = \"\u003Cdiv\u003Esample\u003C\u002Fdiv\u003E\\n\u003Cdiv\u003E&times;\u003C\u002Fdiv\u003E\";\ndiv.addEventListener('click', function(it){\n return console.log('hi', it.target);\n});\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: [[2, 3, node], [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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ block script
1313
+script("https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js")
1414
+script("assets/lib/@plotdb/sheet/dev/index.min.js")
1515
script: :lsc
16+
node =
17+
type: \dom
18+
node: div = document.createElement(\div)
19+
div.setAttribute \class, "d-flex w-100 justify-content-between h-100 align-items-center"
20+
div.innerHTML = """
21+
<div>sample</div>
22+
<div>&times;</div>
23+
"""
24+
div.addEventListener \click, -> console.log \hi, it.target
1625
s = new sheet do
1726
root: '#root'
1827
size:
1928
col: [48,64,92,128].map -> "#{it}px"
2029
row: [48,64,92,128].map -> "#{it}px"
2130
data: [
22-
[1,2,3,4],
31+
[2,3,node],
2332
[5,6,7,8],
2433
[9,10,11,12],
2534
[13,14,15,16]

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

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

0 commit comments

Comments
 (0)