-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-with-comment.html
More file actions
101 lines (88 loc) · 2.84 KB
/
code-with-comment.html
File metadata and controls
101 lines (88 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!--
@license
Copyright (c) 2017 Preview-Code. All rights reserved.
This code may only be used under the BSD style license found in LICENSE.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../comments/add-comment.html">
<link rel="import" href="../comments/comment-list.html">
<link rel="import" href="code-highlighter.html">
<dom-module id="code-with-comment">
<template>
<style>
:host {
display: block
}
[hidden] {
display: none !important;
}
</style>
<code-highlighter id="highlighter" code="{{code}}" language="[[language]]" before-start="{{beforeStart}}" after-start="{{afterStart}}" can-split="true"></code-highlighter>
<comment-list list="[[comments]]"></comment-list>
<add-comment on-add-comment="_addComment" hidden="{{hideAddComments}}" project="[[project]]" line-comment="true"></add-comment>
</template>
<script>
Polymer({
is: 'code-with-comment',
listeners: {
'click-line': '_splitCode',
},
properties: {
code: {
type: String,
notify: true
},
beforeStart: {
type: Number,
notify: true
},
afterStart: {
type: Number,
notify: true
},
hideAddComments: {
type: Boolean,
value: true
},
startLine: Number,
comments: Array,
project: Object,
language: String
},
_splitCode: function (e) {
e.stopPropagation();
var lineInfo = e.detail;
if (lineInfo.lineNumber !== this.$.highlighter.amountLines - 1) {
var x = this.code.split('\n');
this.code = x.splice(lineInfo.lineNumber + 1, this.code.length).join('\n');
var newChunk = {
startLine: this.startLine,
beforeStart: this.beforeStart,
afterStart: this.afterStart,
code: x.join('\n')
};
this.beforeStart = lineInfo.beforeStart;
this.afterStart = lineInfo.afterStart;
this.startLine = this.startLine + lineInfo.lineNumber + 1;
this.fire('add-chunk-with-comment', newChunk);
} else {
this.hideAddComments = false;
}
},
_addComment: function (e) {
var newComment = {};
newComment.body = e.detail.body;
newComment.position = this.startLine + this.$.highlighter.amountLines - 1;
newComment.type = 'line';
var stop = e.detail.stopLoading;
e.stopPropagation();
var submit = function (user) {
newComment.user = user;
newComment.created_at = new Date();
this.push('comments', newComment);
}.bind(this);
this.fire('add-comment', { comment: newComment, stopLoading: stop, onSuccess: submit });
}
});
</script>
</dom-module>