Skip to content

Commit e7eea97

Browse files
authored
Merge pull request #239 from codeitcodes/patch-1
2 parents 4d5d2c2 + 1d2a863 commit e7eea97

File tree

7 files changed

+151
-69
lines changed

7 files changed

+151
-69
lines changed

bottomfloat.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,17 @@ if (isMobile) {
120120
// update on screen resize
121121

122122
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
123-
124-
bottomWrapper.lastHeight = 0;
125-
126-
window.addEventListener('resize', () => {
127-
128-
if (bottomWrapper.lastHeight === window.innerHeight) {
129-
return;
130-
}
131-
132-
bottomWrapper.lastHeight = window.innerHeight;
123+
124+
const landscape = window.matchMedia('(orientation: landscape)');
125+
126+
landscape.addEventListener('change', () => {
133127

134128
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
135-
129+
130+
window.setTimeout(() => {
131+
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
132+
}, 50);
133+
136134
});
137135

138136
}

filebrowser.js

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,33 @@ function createNewFileInHTML() {
24312431
// change selected file
24322432
changeSelectedFile(treeLoc.join(), tempSHA, fileName, encodeUnicode('\r\n'), getFileLang(fileName),
24332433
[0, 0], [0, 0], true);
2434-
2434+
2435+
// close file view if open
2436+
if (liveView.classList.contains('file-open')) {
2437+
2438+
liveView.classList.add('notransition');
2439+
liveView.classList.remove('file-open');
2440+
2441+
onNextFrame(() => {
2442+
2443+
liveView.classList.remove('notransition');
2444+
2445+
});
2446+
2447+
// if on mobile device
2448+
if (isMobile) {
2449+
2450+
// update bottom float
2451+
bottomFloat.classList.remove('file-open');
2452+
2453+
} else {
2454+
2455+
liveToggle.classList.remove('file-open');
2456+
2457+
}
2458+
2459+
}
2460+
24352461
// if on mobile device
24362462
if (isMobile) {
24372463

@@ -2797,17 +2823,26 @@ function toggleSidebar(open) {
27972823
}
27982824

27992825

2800-
function deleteModFileInHTML(fileEl) {
2826+
async function deleteModFileInHTML(fileEl) {
28012827

28022828
const fileSha = getAttr(fileEl, 'sha');
28032829

28042830
deleteModFile(fileSha);
28052831

2832+
28062833
fileEl.classList.remove('modified');
28072834

28082835
if (fileEl.classList.contains('selected')) {
28092836

2810-
loadFileInHTML(fileEl, fileSha);
2837+
const scrollPos = selectedFile.scrollPos;
2838+
2839+
await loadFileInHTML(fileEl, fileSha);
2840+
2841+
// prevent bottom float disappearing on mobile
2842+
if (isMobile) lastScrollTop = scrollPos[1];
2843+
2844+
// scroll to pos in code
2845+
cd.scrollTo(scrollPos[0], scrollPos[1]);
28112846

28122847
}
28132848

@@ -3069,18 +3104,10 @@ function setupEditor() {
30693104

30703105
// update on screen resize
30713106

3072-
let lastWidth;
3073-
3074-
window.addEventListener('resize', () => {
3075-
3076-
if (lastWidth === window.innerWidth) {
3077-
return;
3078-
}
3079-
3080-
lastWidth = window.innerWidth;
3081-
3082-
updateLineNumbersHTML();
3083-
3107+
const landscape = window.matchMedia('(orientation: landscape)');
3108+
3109+
landscape.addEventListener('change', () => {
3110+
onNextFrame(updateLineNumbersHTML);
30843111
});
30853112

30863113

@@ -3155,6 +3182,7 @@ function setupEditor() {
31553182
// get selection language
31563183
let selLang = Prism.util.getLanguage(cursorEl);
31573184
if (selLang == 'javascript') selLang = 'js';
3185+
if (selLang == 'json') selLang = 'js';
31583186
if (selLang == 'markup') selLang = 'html';
31593187

31603188
// find syntax for language
@@ -3165,7 +3193,14 @@ function setupEditor() {
31653193

31663194
// beautify
31673195
beautifierOptions.indent_char = cd.options.tab[0];
3168-
const beautifiedText = beautifyLang(selText, beautifierOptions);
3196+
let beautifiedText = beautifyLang(selText, beautifierOptions);
3197+
3198+
// prevent deleting ending newline when beautifying
3199+
if (selText.endsWith('\n') && !beautifiedText.endsWith('\n')) {
3200+
3201+
beautifiedText += '\n';
3202+
3203+
}
31693204

31703205
// compare current code with new code
31713206
// if the code is different, swap it

full.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ body:not(.mobile) .sidebar .header .title .branch-icon:active {
12831283
text-overflow: clip;
12841284
background: hsl(220deg 86% 64% / 9%);
12851285
box-shadow: 0 0 0 2px hsl(223deg 85% 66% / 70%);
1286-
transition: .18s .04s var(--ease-function);
1286+
transition: .18s var(--ease-function);
12871287
transition-property: background, box-shadow;
12881288
}
12891289

lib/codeit.js

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
codeit.js
4-
v3.1.1
4+
v3.1.2
55
MIT License
66
77
https://codeit.codes
@@ -582,33 +582,72 @@ class CodeitElement extends HTMLElement {
582582
event.preventDefault();
583583

584584
if (event.shiftKey) {
585-
586-
const before = cd.beforeCursor();
587-
588-
// get padding of line
589-
let [padding, start] = getPadding(before);
590-
591-
// get caret pos in text
592-
let pos = cd.getSelection();
593-
594-
if (padding.length > 0) {
595-
596-
const tabLength = cd.options.tab.length;
597-
598-
// remove full length tab
599-
600-
cd.setSelection(start + tabLength);
601-
602-
for (let i = 0; i < tabLength; i++) cd.deleteCurrentSelection();
603-
604-
pos.start -= tabLength;
605-
pos.end -= tabLength;
606-
585+
586+
// get current selection
587+
const s = window.getSelection();
588+
589+
let selContents = s.toString();
590+
591+
// if selection exists
592+
if (!s.isCollapsed) {
593+
594+
let lines = selContents.split('\n');
595+
596+
// run on all lines
597+
lines.forEach((line, index) => {
598+
599+
// if line contains a tab
600+
if (line.startsWith(cd.options.tab)) {
601+
602+
// remove tab from line
603+
lines[index] = line.slice(cd.options.tab.length);
604+
605+
}
606+
607+
});
608+
609+
// join lines
610+
selContents = lines.join('\n');
611+
612+
613+
// delete selection
614+
cd.deleteCurrentSelection();
615+
616+
// insert un-tabbed selection
617+
cd.insert(selContents, { moveToEnd: false });
618+
619+
// get caret pos in text
620+
const pos = cd.getSelection();
621+
607622
// restore pos in text
608-
cd.setSelection(pos.start, pos.end);
609-
623+
cd.setSelection(pos.start, (pos.start + selContents.length));
624+
625+
} else {
626+
627+
let lastLine = cd.beforeCursor().split('\n');
628+
lastLine = lastLine[lastLine.length-1];
629+
630+
// if current line contains a tab
631+
if (lastLine.startsWith(cd.options.tab)) {
632+
633+
// remove tab from line
634+
635+
// get caret pos in text
636+
const pos = cd.getSelection();
637+
638+
// select the tab
639+
cd.setSelection((pos.start - lastLine.length), (pos.start - lastLine.length + cd.options.tab.length));
640+
641+
// delete selection
642+
cd.deleteCurrentSelection();
643+
644+
// restore pos in text
645+
cd.setSelection(pos.start - cd.options.tab.length);
646+
647+
}
648+
610649
}
611-
650+
612651
} else {
613652

614653
// get current selection
@@ -621,29 +660,38 @@ class CodeitElement extends HTMLElement {
621660

622661
if (selContents.includes('\n')) {
623662

624-
// tab lines in selection
663+
// add tabs to selection string
625664
selContents = cd.options.tab + selContents.split('\n').join('\n' + cd.options.tab);
626665

627-
// insert tabbed selection
666+
// delete selection
628667
cd.deleteCurrentSelection();
629-
cd.insert(selContents);
668+
669+
// insert tabbed selection
670+
cd.insert(selContents, { moveToEnd: false });
671+
672+
// get caret pos in text
673+
const pos = cd.getSelection();
674+
675+
// restore pos in text
676+
cd.setSelection(pos.start, (pos.start + selContents.length));
630677

631678
} else {
632679

633680
// tab selection
634-
const sel = cd.getSelection();
635681

636-
cd.setSelection(Math.min(sel.start, sel.end));
682+
// get caret pos in text
683+
const pos = cd.getSelection();
637684

638-
// insert tab at start of selection
639-
cd.insert(cd.options.tab);
685+
const start = Math.min(pos.start, pos.end);
686+
const end = Math.max(pos.start, pos.end);
640687

641-
// reselect text
688+
cd.setSelection(start);
642689

643-
sel.start += cd.options.tab.length;
644-
sel.end += cd.options.tab.length;
690+
// insert tab at start of selection
691+
cd.insert(cd.options.tab, { moveToEnd: false });
645692

646-
cd.setSelection(sel.start, sel.end);
693+
// restore pos in text
694+
cd.setSelection(start, end + cd.options.tab.length);
647695

648696
}
649697

lib/plugins/codeit-autocomplete.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CSSProps.push('flex');
3131
CSSProps.push('grid');
3232
CSSProps.push('gap');
3333
CSSProps.push('background');
34+
CSSProps.push('overflow');
3435
CSSProps.push('-webkit-user-select');
3536

3637
const maxChar = 0;//4;

utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ if (isEmbed) {
311311

312312
}
313313

314-
window.addEventListener('resize', () => {
315-
316-
isLandscape = window.matchMedia('(orientation: landscape)').matches;
314+
window.matchMedia('(orientation: landscape)').addEventListener('change', (e) => {
315+
316+
isLandscape = e.matches;
317317

318318
});
319319

worker/client-channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
// update worker name when updating worker
7-
const WORKER_NAME = 'codeit-worker-v607';
7+
const WORKER_NAME = 'codeit-worker-v612';
88

99

1010
// internal paths

0 commit comments

Comments
 (0)