-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg.html
More file actions
187 lines (175 loc) · 6.29 KB
/
pg.html
File metadata and controls
187 lines (175 loc) · 6.29 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>CodePen風エディタ</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: system-ui, sans-serif; background: #1e1e2e; color: #cdd6f4; height: 100vh; display: flex; flex-direction: column; }
header { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px; background: #181825; border-bottom: 1px solid #313244; }
header h1 { font-size: 16px; font-weight: 600; }
.toolbar { display: flex; gap: 8px; }
button { background: #89b4fa; color: #1e1e2e; border: none; padding: 6px 16px; border-radius: 6px; font-size: 13px; cursor: pointer; font-weight: 600; }
button:hover { background: #74c7ec; }
button.secondary { background: #45475a; color: #cdd6f4; }
button.secondary:hover { background: #585b70; }
.main { display: flex; flex: 1; min-height: 0; }
.editor-pane { flex: 1; display: flex; flex-direction: column; border-right: 2px solid #313244; }
.preview-pane { flex: 1; display: flex; flex-direction: column; }
.pane-header { padding: 6px 12px; background: #181825; font-size: 12px; font-weight: 600; color: #a6adc8; text-transform: uppercase; letter-spacing: 0.5px; }
textarea { flex: 1; background: #1e1e2e; color: #cdd6f4; border: none; padding: 12px; font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace; font-size: 14px; line-height: 1.6; resize: none; tab-size: 2; outline: none; }
iframe { flex: 1; border: none; background: white; }
.layout-toggle { display: flex; gap: 4px; }
.layout-toggle button { padding: 4px 8px; font-size: 11px; }
@media (max-width: 768px) { .main { flex-direction: column; } .editor-pane { border-right: none; border-bottom: 2px solid #313244; } }
</style>
</head>
<body>
<header>
<h1>CodePen</h1>
<div class="toolbar">
<button onclick="run()">▶ 実行</button>
<button class="secondary" onclick="clearEditor()">クリア</button>
<button class="secondary" onclick="saveFile()">保存</button>
<button class="secondary" onclick="openFile()">開く</button>
<input type="file" id="fileInput" accept=".html,.htm" style="display:none" onchange="handleOpen(event)">
<button class="secondary" onclick="copyCode()">コピー</button>
<button class="secondary" onclick="toggleLayout()">横/縦</button>
</div>
</header>
<div class="main" id="main">
<div class="editor-pane">
<div class="pane-header">HTML / JavaScript</div>
<textarea id="editor" spellcheck="false" placeholder="ここに HTML を貼り付けて「実行」を押してください"></textarea>
</div>
<div class="preview-pane">
<div class="pane-header">プレビュー</div>
<iframe id="preview"></iframe>
</div>
</div>
<script>
const editor = document.getElementById('editor');
const preview = document.getElementById('preview');
// 実行
function run() {
const code = editor.value;
const doc = preview.contentDocument || preview.contentWindow.document;
doc.open();
doc.write(code);
doc.close();
}
// クリア
function clearEditor() {
editor.value = '';
const doc = preview.contentDocument || preview.contentWindow.document;
doc.open();
doc.write('');
doc.close();
}
// コピー
function copyCode() {
navigator.clipboard.writeText(editor.value).then(() => {
const btn = event.target;
const orig = btn.textContent;
btn.textContent = 'コピーしました';
setTimeout(() => btn.textContent = orig, 1500);
});
}
// HTMLファイルとして保存
function saveFile() {
const blob = new Blob([editor.value], { type: 'text/html' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'index.html';
a.click();
URL.revokeObjectURL(a.href);
}
// ファイルを開く
function openFile() {
document.getElementById('fileInput').click();
}
function handleOpen(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
editor.value = e.target.result;
run();
};
reader.readAsText(file);
event.target.value = ''; // 同じファイルを再度開けるようにリセット
}
// レイアウト切り替え
function toggleLayout() {
const main = document.getElementById('main');
main.style.flexDirection = main.style.flexDirection === 'column' ? 'row' : 'column';
}
// Tab キーでインデント
editor.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
e.preventDefault();
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.substring(0, start) + ' ' + editor.value.substring(end);
editor.selectionStart = editor.selectionEnd = start + 2;
}
});
// Ctrl+Enter / Cmd+Enter で実行
editor.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
run();
}
});
// サンプルコード
editor.value = `<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f0f4f8;
}
.card {
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
text-align: center;
}
#message { font-size: 24px; margin: 20px 0; }
button {
background: #4299e1;
color: white;
border: none;
padding: 12px 32px;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="card">
<h1>ボタンを押してみて</h1>
<p id="message">ここが変わります</p>
<button onclick="changeMessage()">押す</button>
</div>
<script>
function changeMessage() {
document.getElementById('message').textContent = 'こんにちは!';
}
<\/script>
</body>
</html>`;
// 初回実行
run();
</script>
</body>
</html>