Skip to content

Commit 2ad67a9

Browse files
committed
partially fixed js for 1.0, fixed style changes from js, added code to force shadowDOM
1 parent 1c7f89f commit 2ad67a9

File tree

3 files changed

+83
-61
lines changed

3 files changed

+83
-61
lines changed

index.html

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<title>PrettyCode</title>
55
<!--<link rel="stylesheet" href="styles.css">-->
66
<link rel="stylesheet" href="fonts.css">
7+
8+
<script src="setShadow.js"></script>
9+
710
<link rel="import" href="bower_components/polymer/polymer.html">
811
<link rel="import" href="bower_components/iron-flex-layout/classes/iron-flex-layout.html">
912
<link rel="import" href="bower_components/prettify-element/prettify-element.html">
@@ -21,7 +24,7 @@
2124
<div class="fit layout vertical">
2225
<div class="horizontal layout" style="margin-top:5px">
2326
<div style="margin:5px">Light</div>
24-
<paper-toggle-button id="ptbTheme"
27+
<paper-toggle-button id="ptbTheme" checked="{{_selTheme(theme)}}"
2528
style="margin:5px"
2629
on-change="chTheme"></paper-toggle-button>
2730
<div style="margin:5px">Dark</div>
@@ -45,19 +48,20 @@
4548
</paper-dropdown-menu>
4649
-->
4750
<paper-input id="ptDimension" label="Font size (pt)"
48-
value="{{fontPt}}" on-change="ptChange"
51+
value="{{fontPt}}" on-value-changed="ptChange"
52+
type="number" auto-validate
4953
always-float-label
5054
style="margin-left:10px;" ></paper-input>
5155
</div>
52-
<paper-textarea id="agTa" value="{{code}}">
56+
<paper-textarea id="agTa" value="{{code}}" on-value-changed="codeChanged">
5357
</paper-textarea>
5458
<div id="slidesWarnings">
5559
</div>
5660
<prettify-element id="destination"
5761
text="{{code}}"
5862
language="{{language}}"
5963
on-tap="selPrettyCode"
60-
class="flex"
64+
class="flex "
6165
style="overflow:auto"></prettify-element>
6266
</div>
6367
</template>

main.js

+73-57
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,27 @@ chrome.storage.sync.get(
1010
mainContent.theme = localStorage.theme || 'light';
1111
mainContent.fontPt = localStorage.fontPt || 14;
1212
});
13-
mainContent.addEventListener('template-bound', function() {
13+
mainContent.addEventListener('dom-change', function() {
1414
//Set the font-size
15-
mainContent.ptChange();
15+
mainContent.ptChange({detail: mainContent.fontPt});
1616

1717
//Now that the template is bound update the code in the textArea
1818
//mainContent.$.codeValue = mainContent.code;
1919
//mainContent.$.agTa.update(mainContent.$.taCode); //Update the autoGrowArea;
2020

21-
//Add a change listener to the textArea
22-
mainContent.$.taCode.addEventListener('input', function() {
23-
mainContent.code = mainContent.$.taCode.value;
24-
chrome.storage.sync.set({'theCode': mainContent.code}, function() {
25-
//Nothing to do
26-
});
27-
//Code Changed, run the validation for slides
28-
mainContent.validateForSlides();
29-
});
30-
3121
//Find the label of the selected language to set it on the paper-dropdown-menu
32-
var mnItems = document.querySelectorAll('paper-item');
22+
/*var mnItems = document.querySelectorAll('paper-item');
3323
[].some.call(mnItems, function(mnItem) {
3424
if (mnItem.dataset.value == mainContent.language) {
3525
//Item found, update the selectedItem to change the label
3626
mainContent.$.pdmLanguage.selectedItemLabel = mnItem.innerText;
3727
return true;
3828
}
3929
return false;
40-
});
30+
});*/
4131

4232
//Select the theme on the slider
43-
mainContent.$.ptbTheme.checked = (mainContent.theme == 'dark');
33+
//mainContent.$.ptbTheme.checked = (mainContent.theme == 'dark');
4434

4535
//Set the theme and lang on all the components
4636
setThemeAndLang();
@@ -49,13 +39,33 @@ mainContent.addEventListener('template-bound', function() {
4939
mainContent.validateForSlides();
5040
});
5141

42+
mainContent._selTheme = function(theme){
43+
var tmpTheme = theme || 'light';
44+
45+
return (tmpTheme == 'dark');
46+
47+
};
48+
49+
mainContent.codeChanged = function(newVal) {
50+
51+
chrome.storage.sync.set({'theCode': mainContent.code}, function() {
52+
//Nothing to do
53+
});
54+
//Code Changed, run the validation for slides
55+
mainContent.validateForSlides();
56+
57+
};
58+
5259
var setThemeAndLang = function() {
5360
//Change the classes on the prettyprint element accordingly
54-
document.body.className = 'theme-' + mainContent.theme;
61+
var tmpTheme = mainContent.theme || 'light';
62+
63+
document.body.className = 'theme-' + tmpTheme;
5564

56-
mainContent.$.taCode.className = 'theme-' + mainContent.theme;
65+
//mainContent.$.taCode.className = 'theme-' + mainContent.theme;
5766

58-
mainContent.$.destination.className = 'theme-' + mainContent.theme;
67+
mainContent.$.destination.className = 'flex theme-' + tmpTheme;
68+
Polymer.updateStyles();
5969

6070
//Change the language class if needed
6171
/*if (mainContent.lang != '--') {
@@ -97,62 +107,68 @@ var ptToPx = function(valPt) {
97107
return (16 / 12) * valPt;//return the font-size in px from the ptValue
98108
};
99109

100-
mainContent.ptChange = function() {
101-
//TODO:Validate the value before saving it and using it to calculate the px value
102-
103-
chrome.storage.sync.set({'fontPt': mainContent.fontPt}, function() {
110+
mainContent.ptChange = function(newVal) {
111+
112+
chrome.storage.sync.set({'fontPt': newVal.detail.value}, function() {
104113
//Nothing to do
105114
});
106115

107116
//Get the px approximate size
108-
var fontPx = ptToPx(mainContent.fontPt) + 'px';
117+
mainContent.fontPx = ptToPx(newVal.detail.value) + 'px';
109118

110119
//AutoGrow Text Area
111-
mainContent.$.agTa.style.fontSize = fontPx;
120+
mainContent.$.agTa.style.fontSize = mainContent.fontPx;
112121
//Text Area
113-
mainContent.$.taCode.style.fontSize = fontPx;
122+
//mainContent.$.taCode.style.fontSize = fontPx;
123+
114124
//Pre Element
115-
var preElement = mainContent.$.destination.shadowRoot.querySelector('pre');
116-
preElement.style.fontSize = fontPx;
125+
if (mainContent.$.destination.shadowRoot) {
126+
var preElement = mainContent.$.destination.shadowRoot.querySelector('pre');
127+
preElement.style.fontSize = mainContent.fontPx;
128+
Polymer.updateStyles();
129+
}
117130

118131
};
119132

120133
mainContent.selPrettyCode = function(sender) {
121-
//Get the pre element inside the prettyfy-element
122-
var preElement = sender.currentTarget.shadowRoot.querySelector('pre');
123-
124-
//Select the text range
125-
var doc = document;
126-
var selection = window.getSelection();
127-
var range = doc.createRange();
128-
range.selectNodeContents(preElement);
129-
selection.removeAllRanges();
130-
selection.addRange(range);
131-
134+
//Get the pre element inside the prettify-element
135+
if (mainContent.$.destination.shadowRoot) {
136+
var preElement = mainContent.$.destination.shadowRoot.querySelector('pre');
137+
//Select the text range
138+
var doc = document;
139+
var selection = window.getSelection();
140+
var range = doc.createRange();
141+
range.selectNodeContents(preElement);
142+
selection.removeAllRanges();
143+
selection.addRange(range);
144+
}
132145
};
133146

134147
mainContent.validateForSlides = function() {
135148
var divW = document.querySelector('#slidesWarnings');
136-
var warn = [];
137-
138-
var MAX_LINES = 20;
139-
if ((mainContent.code.match(/\n/g) || []).length >= MAX_LINES) {
140-
warn.push('More than ' + MAX_LINES +
141-
' lines of code will be hard to read on a slide.');
142-
}
143-
144-
var lines = mainContent.code.split('\n') || [];
145-
var MAX_LINE_LENGTH = 80;
146-
for (var i = 0; i < lines.length; i++) {
147-
if (lines[i].length > MAX_LINE_LENGTH) {
148-
warn.push('Line ' + (i + 1) + ' has more than ' +
149-
MAX_LINE_LENGTH + ' characters!');
149+
150+
if (divW){
151+
var warn = [];
152+
153+
var MAX_LINES = 20;
154+
if ((mainContent.code.match(/\n/g) || []).length >= MAX_LINES) {
155+
warn.push('More than ' + MAX_LINES +
156+
' lines of code will be hard to read on a slide.');
157+
}
158+
159+
var lines = mainContent.code.split('\n') || [];
160+
var MAX_LINE_LENGTH = 80;
161+
for (var i = 0; i < lines.length; i++) {
162+
if (lines[i].length > MAX_LINE_LENGTH) {
163+
warn.push('Line ' + (i + 1) + ' has more than ' +
164+
MAX_LINE_LENGTH + ' characters!');
165+
}
166+
}
167+
if (warn.length > 0) {
168+
divW.innerHTML = warn.join('<br>');
169+
}else {
170+
divW.innerHTML = 'Perfect code for slides';
150171
}
151-
}
152-
if (warn.length > 0) {
153-
divW.innerHTML = warn.join('<br>');
154-
}else {
155-
divW.innerHTML = 'Perfect code for slides';
156172
}
157173
};
158174

setShadow.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window.Polymer = window.Polymer || {};
2+
window.Polymer.dom = 'shadow';

0 commit comments

Comments
 (0)