sunny-html-editor 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/html-editor.js +55 -55
- package/package.json +1 -1
package/html-editor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Sunny HTML Editor - 軽量WYSIWYGエディタ
|
|
3
|
-
* @version 1.0.
|
|
3
|
+
* @version 1.0.3
|
|
4
4
|
*/
|
|
5
5
|
document.addEventListener('DOMContentLoaded', function() {
|
|
6
6
|
var container = document.getElementById('editorContainer');
|
|
@@ -43,13 +43,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// ========== HTML保存 ==========
|
|
46
|
-
saveHtmlBtn.addEventListener('click', function() {
|
|
46
|
+
if (saveHtmlBtn) saveHtmlBtn.addEventListener('click', function() {
|
|
47
47
|
var wasEditMode = isEditMode;
|
|
48
|
-
if (isEditMode) editModeBtn.click();
|
|
48
|
+
if (isEditMode && editModeBtn) editModeBtn.click();
|
|
49
49
|
|
|
50
50
|
var html = '<!DOCTYPE html>\n' + document.documentElement.outerHTML;
|
|
51
51
|
|
|
52
|
-
if (wasEditMode) editModeBtn.click();
|
|
52
|
+
if (wasEditMode && editModeBtn) editModeBtn.click();
|
|
53
53
|
|
|
54
54
|
var blob = new Blob([html], { type: 'text/html; charset=utf-8' });
|
|
55
55
|
var url = URL.createObjectURL(blob);
|
|
@@ -72,7 +72,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
// ========== Markdown出力 ==========
|
|
75
|
-
exportMdBtn.addEventListener('click', function() {
|
|
75
|
+
if (exportMdBtn) exportMdBtn.addEventListener('click', function() {
|
|
76
76
|
var md = htmlToMarkdown();
|
|
77
77
|
|
|
78
78
|
navigator.clipboard.writeText(md).then(function() {
|
|
@@ -85,7 +85,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
// ========== HTMLコピー ==========
|
|
88
|
-
exportHtmlBtn.addEventListener('click', function() {
|
|
88
|
+
if (exportHtmlBtn) exportHtmlBtn.addEventListener('click', function() {
|
|
89
89
|
var html = getPlainHtml();
|
|
90
90
|
|
|
91
91
|
navigator.clipboard.writeText(html).then(function() {
|
|
@@ -436,7 +436,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
436
436
|
if (newWidth < 200) newWidth = 200;
|
|
437
437
|
if (newWidth > 400) newWidth = 400;
|
|
438
438
|
s.style.width = newWidth + 'px';
|
|
439
|
-
main.style.marginLeft = newWidth + 'px';
|
|
439
|
+
if (main) main.style.marginLeft = newWidth + 'px';
|
|
440
440
|
updateResizerPosition();
|
|
441
441
|
});
|
|
442
442
|
|
|
@@ -448,23 +448,23 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
448
448
|
}
|
|
449
449
|
});
|
|
450
450
|
|
|
451
|
-
toggleSidebarBtn.addEventListener('click', function() {
|
|
451
|
+
if (toggleSidebarBtn) toggleSidebarBtn.addEventListener('click', function() {
|
|
452
452
|
var s = document.querySelector('.sidebar');
|
|
453
453
|
var r = document.querySelector('.resizer');
|
|
454
454
|
|
|
455
|
-
if (sidebarExists) {
|
|
455
|
+
if (sidebarExists && s && r) {
|
|
456
456
|
savedWidth = s.offsetWidth;
|
|
457
457
|
sidebarHtml = s.outerHTML;
|
|
458
458
|
resizerHtml = r.outerHTML;
|
|
459
459
|
s.remove();
|
|
460
460
|
r.remove();
|
|
461
|
-
main.style.marginLeft = '0';
|
|
461
|
+
if (main) main.style.marginLeft = '0';
|
|
462
462
|
sidebarExists = false;
|
|
463
|
-
} else {
|
|
463
|
+
} else if (!sidebarExists && main) {
|
|
464
464
|
main.insertAdjacentHTML('beforebegin', sidebarHtml);
|
|
465
465
|
main.insertAdjacentHTML('beforebegin', resizerHtml);
|
|
466
466
|
var newSidebar = document.querySelector('.sidebar');
|
|
467
|
-
newSidebar.style.width = savedWidth + 'px';
|
|
467
|
+
if (newSidebar) newSidebar.style.width = savedWidth + 'px';
|
|
468
468
|
main.style.marginLeft = savedWidth + 'px';
|
|
469
469
|
setupResizer();
|
|
470
470
|
updateResizerPosition();
|
|
@@ -514,7 +514,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
514
514
|
}
|
|
515
515
|
|
|
516
516
|
// ========== 編集モード切り替え ==========
|
|
517
|
-
editModeBtn.addEventListener('click', function() {
|
|
517
|
+
if (editModeBtn) editModeBtn.addEventListener('click', function() {
|
|
518
518
|
isEditMode = !isEditMode;
|
|
519
519
|
if (isEditMode) {
|
|
520
520
|
container.classList.add('edit-mode');
|
|
@@ -625,19 +625,19 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
625
625
|
container.addEventListener('contextmenu', function(e) {
|
|
626
626
|
if (!isEditMode) return;
|
|
627
627
|
|
|
628
|
-
contextMenu.classList.remove('show');
|
|
629
|
-
h1ContextMenu.classList.remove('show');
|
|
630
|
-
headingContextMenu.classList.remove('show');
|
|
631
|
-
paragraphContextMenu.classList.remove('show');
|
|
632
|
-
listContextMenu.classList.remove('show');
|
|
633
|
-
preContextMenu.classList.remove('show');
|
|
634
|
-
blockquoteContextMenu.classList.remove('show');
|
|
628
|
+
if (contextMenu) contextMenu.classList.remove('show');
|
|
629
|
+
if (h1ContextMenu) h1ContextMenu.classList.remove('show');
|
|
630
|
+
if (headingContextMenu) headingContextMenu.classList.remove('show');
|
|
631
|
+
if (paragraphContextMenu) paragraphContextMenu.classList.remove('show');
|
|
632
|
+
if (listContextMenu) listContextMenu.classList.remove('show');
|
|
633
|
+
if (preContextMenu) preContextMenu.classList.remove('show');
|
|
634
|
+
if (blockquoteContextMenu) blockquoteContextMenu.classList.remove('show');
|
|
635
635
|
|
|
636
636
|
var x = e.clientX;
|
|
637
637
|
var y = e.clientY;
|
|
638
638
|
|
|
639
639
|
var h1 = e.target.closest('h1');
|
|
640
|
-
if (h1) {
|
|
640
|
+
if (h1 && h1ContextMenu) {
|
|
641
641
|
e.preventDefault();
|
|
642
642
|
contextTargetH1 = h1;
|
|
643
643
|
showContextMenu(h1ContextMenu, x, y);
|
|
@@ -645,7 +645,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
645
645
|
}
|
|
646
646
|
|
|
647
647
|
var heading = e.target.closest('h2, h3, h4');
|
|
648
|
-
if (heading) {
|
|
648
|
+
if (heading && headingContextMenu) {
|
|
649
649
|
e.preventDefault();
|
|
650
650
|
contextTargetHeading = heading;
|
|
651
651
|
showContextMenu(headingContextMenu, x, y);
|
|
@@ -653,7 +653,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
653
653
|
}
|
|
654
654
|
|
|
655
655
|
var cell = e.target.closest('td, th');
|
|
656
|
-
if (cell) {
|
|
656
|
+
if (cell && contextMenu) {
|
|
657
657
|
e.preventDefault();
|
|
658
658
|
contextTargetCell = cell;
|
|
659
659
|
showContextMenu(contextMenu, x, y);
|
|
@@ -661,7 +661,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
661
661
|
}
|
|
662
662
|
|
|
663
663
|
var li = e.target.closest('li');
|
|
664
|
-
if (li) {
|
|
664
|
+
if (li && listContextMenu) {
|
|
665
665
|
e.preventDefault();
|
|
666
666
|
contextTargetLi = li;
|
|
667
667
|
contextTargetList = li.closest('ul, ol');
|
|
@@ -670,7 +670,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
670
670
|
}
|
|
671
671
|
|
|
672
672
|
var pre = e.target.closest('pre');
|
|
673
|
-
if (pre) {
|
|
673
|
+
if (pre && preContextMenu) {
|
|
674
674
|
e.preventDefault();
|
|
675
675
|
contextTargetPre = pre;
|
|
676
676
|
showContextMenu(preContextMenu, x, y);
|
|
@@ -678,7 +678,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
678
678
|
}
|
|
679
679
|
|
|
680
680
|
var blockquote = e.target.closest('blockquote');
|
|
681
|
-
if (blockquote) {
|
|
681
|
+
if (blockquote && blockquoteContextMenu) {
|
|
682
682
|
e.preventDefault();
|
|
683
683
|
contextTargetBlockquote = blockquote;
|
|
684
684
|
showContextMenu(blockquoteContextMenu, x, y);
|
|
@@ -686,7 +686,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
686
686
|
}
|
|
687
687
|
|
|
688
688
|
var paragraph = e.target.closest('p');
|
|
689
|
-
if (paragraph) {
|
|
689
|
+
if (paragraph && paragraphContextMenu) {
|
|
690
690
|
e.preventDefault();
|
|
691
691
|
contextTargetParagraph = paragraph;
|
|
692
692
|
showContextMenu(paragraphContextMenu, x, y);
|
|
@@ -695,24 +695,24 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
695
695
|
});
|
|
696
696
|
|
|
697
697
|
document.addEventListener('click', function() {
|
|
698
|
-
contextMenu.classList.remove('show');
|
|
699
|
-
h1ContextMenu.classList.remove('show');
|
|
700
|
-
headingContextMenu.classList.remove('show');
|
|
701
|
-
paragraphContextMenu.classList.remove('show');
|
|
702
|
-
listContextMenu.classList.remove('show');
|
|
703
|
-
preContextMenu.classList.remove('show');
|
|
704
|
-
blockquoteContextMenu.classList.remove('show');
|
|
698
|
+
if (contextMenu) contextMenu.classList.remove('show');
|
|
699
|
+
if (h1ContextMenu) h1ContextMenu.classList.remove('show');
|
|
700
|
+
if (headingContextMenu) headingContextMenu.classList.remove('show');
|
|
701
|
+
if (paragraphContextMenu) paragraphContextMenu.classList.remove('show');
|
|
702
|
+
if (listContextMenu) listContextMenu.classList.remove('show');
|
|
703
|
+
if (preContextMenu) preContextMenu.classList.remove('show');
|
|
704
|
+
if (blockquoteContextMenu) blockquoteContextMenu.classList.remove('show');
|
|
705
705
|
});
|
|
706
706
|
|
|
707
707
|
document.addEventListener('keydown', function(e) {
|
|
708
708
|
if (e.key === 'Escape') {
|
|
709
|
-
contextMenu.classList.remove('show');
|
|
710
|
-
h1ContextMenu.classList.remove('show');
|
|
711
|
-
headingContextMenu.classList.remove('show');
|
|
712
|
-
paragraphContextMenu.classList.remove('show');
|
|
713
|
-
listContextMenu.classList.remove('show');
|
|
714
|
-
preContextMenu.classList.remove('show');
|
|
715
|
-
blockquoteContextMenu.classList.remove('show');
|
|
709
|
+
if (contextMenu) contextMenu.classList.remove('show');
|
|
710
|
+
if (h1ContextMenu) h1ContextMenu.classList.remove('show');
|
|
711
|
+
if (headingContextMenu) headingContextMenu.classList.remove('show');
|
|
712
|
+
if (paragraphContextMenu) paragraphContextMenu.classList.remove('show');
|
|
713
|
+
if (listContextMenu) listContextMenu.classList.remove('show');
|
|
714
|
+
if (preContextMenu) preContextMenu.classList.remove('show');
|
|
715
|
+
if (blockquoteContextMenu) blockquoteContextMenu.classList.remove('show');
|
|
716
716
|
}
|
|
717
717
|
|
|
718
718
|
// ↓↑キーで編集可能要素間を移動(複数行対応)
|
|
@@ -785,15 +785,15 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
785
785
|
switch (e.key.toLowerCase()) {
|
|
786
786
|
case 'b':
|
|
787
787
|
e.preventDefault();
|
|
788
|
-
toggleSidebarBtn.click();
|
|
788
|
+
if (toggleSidebarBtn) toggleSidebarBtn.click();
|
|
789
789
|
break;
|
|
790
790
|
case 'e':
|
|
791
791
|
e.preventDefault();
|
|
792
|
-
editModeBtn.click();
|
|
792
|
+
if (editModeBtn) editModeBtn.click();
|
|
793
793
|
break;
|
|
794
794
|
case 'm':
|
|
795
795
|
e.preventDefault();
|
|
796
|
-
exportMdBtn.click();
|
|
796
|
+
if (exportMdBtn) exportMdBtn.click();
|
|
797
797
|
break;
|
|
798
798
|
}
|
|
799
799
|
}
|
|
@@ -902,7 +902,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
902
902
|
}
|
|
903
903
|
});
|
|
904
904
|
|
|
905
|
-
contextMenu.addEventListener('click', function(e) {
|
|
905
|
+
if (contextMenu) contextMenu.addEventListener('click', function(e) {
|
|
906
906
|
e.stopPropagation();
|
|
907
907
|
var item = e.target.closest('.context-menu-item');
|
|
908
908
|
if (!item || !contextTargetCell) return;
|
|
@@ -990,7 +990,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
990
990
|
contextTargetCell = null;
|
|
991
991
|
});
|
|
992
992
|
|
|
993
|
-
h1ContextMenu.addEventListener('click', function(e) {
|
|
993
|
+
if (h1ContextMenu) h1ContextMenu.addEventListener('click', function(e) {
|
|
994
994
|
e.stopPropagation();
|
|
995
995
|
var item = e.target.closest('.context-menu-item');
|
|
996
996
|
if (!item || !contextTargetH1) return;
|
|
@@ -1026,7 +1026,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1026
1026
|
contextTargetH1 = null;
|
|
1027
1027
|
});
|
|
1028
1028
|
|
|
1029
|
-
headingContextMenu.addEventListener('click', function(e) {
|
|
1029
|
+
if (headingContextMenu) headingContextMenu.addEventListener('click', function(e) {
|
|
1030
1030
|
e.stopPropagation();
|
|
1031
1031
|
var item = e.target.closest('.context-menu-item');
|
|
1032
1032
|
if (!item || !contextTargetHeading) return;
|
|
@@ -1089,7 +1089,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1089
1089
|
contextTargetHeading = null;
|
|
1090
1090
|
});
|
|
1091
1091
|
|
|
1092
|
-
paragraphContextMenu.addEventListener('click', function(e) {
|
|
1092
|
+
if (paragraphContextMenu) paragraphContextMenu.addEventListener('click', function(e) {
|
|
1093
1093
|
e.stopPropagation();
|
|
1094
1094
|
var item = e.target.closest('.context-menu-item');
|
|
1095
1095
|
if (!item || !contextTargetParagraph) return;
|
|
@@ -1134,7 +1134,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1134
1134
|
contextTargetParagraph = null;
|
|
1135
1135
|
});
|
|
1136
1136
|
|
|
1137
|
-
listContextMenu.addEventListener('click', function(e) {
|
|
1137
|
+
if (listContextMenu) listContextMenu.addEventListener('click', function(e) {
|
|
1138
1138
|
e.stopPropagation();
|
|
1139
1139
|
var item = e.target.closest('.context-menu-item');
|
|
1140
1140
|
if (!item || !contextTargetList) return;
|
|
@@ -1194,7 +1194,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1194
1194
|
contextTargetLi = null;
|
|
1195
1195
|
});
|
|
1196
1196
|
|
|
1197
|
-
preContextMenu.addEventListener('click', function(e) {
|
|
1197
|
+
if (preContextMenu) preContextMenu.addEventListener('click', function(e) {
|
|
1198
1198
|
e.stopPropagation();
|
|
1199
1199
|
var item = e.target.closest('.context-menu-item');
|
|
1200
1200
|
if (!item || !contextTargetPre) return;
|
|
@@ -1239,7 +1239,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1239
1239
|
contextTargetPre = null;
|
|
1240
1240
|
});
|
|
1241
1241
|
|
|
1242
|
-
blockquoteContextMenu.addEventListener('click', function(e) {
|
|
1242
|
+
if (blockquoteContextMenu) blockquoteContextMenu.addEventListener('click', function(e) {
|
|
1243
1243
|
e.stopPropagation();
|
|
1244
1244
|
var item = e.target.closest('.context-menu-item');
|
|
1245
1245
|
if (!item || !contextTargetBlockquote) return;
|
|
@@ -1364,9 +1364,9 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1364
1364
|
}
|
|
1365
1365
|
|
|
1366
1366
|
// ========== PDF出力 ==========
|
|
1367
|
-
exportPdfBtn.addEventListener('click', async function() {
|
|
1367
|
+
if (exportPdfBtn) exportPdfBtn.addEventListener('click', async function() {
|
|
1368
1368
|
var wasEditMode = isEditMode;
|
|
1369
|
-
if (isEditMode) editModeBtn.click();
|
|
1369
|
+
if (isEditMode && editModeBtn) editModeBtn.click();
|
|
1370
1370
|
|
|
1371
1371
|
exportPdfBtn.textContent = '...';
|
|
1372
1372
|
|
|
@@ -1553,7 +1553,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1553
1553
|
}
|
|
1554
1554
|
|
|
1555
1555
|
exportPdfBtn.textContent = '📕';
|
|
1556
|
-
if (wasEditMode) editModeBtn.click();
|
|
1556
|
+
if (wasEditMode && editModeBtn) editModeBtn.click();
|
|
1557
1557
|
});
|
|
1558
1558
|
|
|
1559
1559
|
// ========== Excel出力 ==========
|
|
@@ -1563,7 +1563,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
|
1563
1563
|
body: 12
|
|
1564
1564
|
};
|
|
1565
1565
|
|
|
1566
|
-
exportExcelBtn.addEventListener('click', async function() {
|
|
1566
|
+
if (exportExcelBtn) exportExcelBtn.addEventListener('click', async function() {
|
|
1567
1567
|
try {
|
|
1568
1568
|
await convertToExcel();
|
|
1569
1569
|
} catch (error) {
|