quikdown 1.1.0 → 1.1.1
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/README.md +11 -3
- package/dist/quikdown.cjs +2 -2
- package/dist/quikdown.dark.css +1 -1
- package/dist/quikdown.esm.js +2 -2
- package/dist/quikdown.esm.min.js +2 -2
- package/dist/quikdown.light.css +1 -1
- package/dist/quikdown.umd.js +2 -2
- package/dist/quikdown.umd.min.js +2 -2
- package/dist/quikdown_bd.cjs +2 -2
- package/dist/quikdown_bd.esm.js +2 -2
- package/dist/quikdown_bd.esm.min.js +2 -2
- package/dist/quikdown_bd.umd.js +2 -2
- package/dist/quikdown_bd.umd.min.js +2 -2
- package/dist/quikdown_edit.cjs +1860 -75
- package/dist/quikdown_edit.esm.js +1860 -75
- package/dist/quikdown_edit.esm.min.js +3 -4
- package/dist/quikdown_edit.esm.min.js.map +1 -1
- package/dist/quikdown_edit.umd.js +1860 -75
- package/dist/quikdown_edit.umd.min.js +3 -4
- package/dist/quikdown_edit.umd.min.js.map +1 -1
- package/package.json +5 -5
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Quikdown Editor - Drop-in Markdown Parser
|
|
3
|
-
* @version 1.1.
|
|
3
|
+
* @version 1.1.1
|
|
4
4
|
* @license BSD-2-Clause
|
|
5
5
|
* @copyright DeftIO 2025
|
|
6
6
|
*/
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
// Version will be injected at build time
|
|
27
|
-
const quikdownVersion = '1.1.
|
|
27
|
+
const quikdownVersion = '1.1.1';
|
|
28
28
|
|
|
29
29
|
// Constants for reuse
|
|
30
30
|
const CLASS_PREFIX = 'quikdown-';
|
|
@@ -1070,6 +1070,1472 @@
|
|
|
1070
1070
|
window.quikdown_bd = quikdown_bd;
|
|
1071
1071
|
}
|
|
1072
1072
|
|
|
1073
|
+
/**
|
|
1074
|
+
* Rich copy functionality for QuikdownEditor
|
|
1075
|
+
* Handles copying rendered content with proper formatting for pasting into rich text editors
|
|
1076
|
+
*/
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* Get platform information
|
|
1080
|
+
* @returns {string} The detected platform: 'macos', 'windows', 'linux', or 'unknown'
|
|
1081
|
+
*/
|
|
1082
|
+
function getPlatform() {
|
|
1083
|
+
const platform = navigator.platform?.toLowerCase() || '';
|
|
1084
|
+
const userAgent = navigator.userAgent?.toLowerCase() || '';
|
|
1085
|
+
|
|
1086
|
+
if (platform.includes('mac') || userAgent.includes('mac')) {
|
|
1087
|
+
return 'macos';
|
|
1088
|
+
} else if (userAgent.includes('windows')) {
|
|
1089
|
+
return 'windows';
|
|
1090
|
+
} else if (userAgent.includes('linux')) {
|
|
1091
|
+
return 'linux';
|
|
1092
|
+
}
|
|
1093
|
+
return 'unknown';
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
/**
|
|
1097
|
+
* Copy to clipboard using HTML selection fallback (for Safari)
|
|
1098
|
+
* Uses div with selection to preserve HTML formatting
|
|
1099
|
+
* @param {string} html - HTML content to copy
|
|
1100
|
+
* @returns {boolean} Success status
|
|
1101
|
+
*/
|
|
1102
|
+
function copyToClipboard(html) {
|
|
1103
|
+
let tempDiv;
|
|
1104
|
+
let result;
|
|
1105
|
+
|
|
1106
|
+
try {
|
|
1107
|
+
// Use a div instead of textarea to preserve HTML formatting
|
|
1108
|
+
tempDiv = document.createElement('div');
|
|
1109
|
+
tempDiv.style.position = 'fixed';
|
|
1110
|
+
tempDiv.style.left = '-9999px';
|
|
1111
|
+
tempDiv.style.top = '0';
|
|
1112
|
+
tempDiv.style.width = '1px';
|
|
1113
|
+
tempDiv.style.height = '1px';
|
|
1114
|
+
tempDiv.style.overflow = 'hidden';
|
|
1115
|
+
tempDiv.innerHTML = html;
|
|
1116
|
+
|
|
1117
|
+
document.body.appendChild(tempDiv);
|
|
1118
|
+
|
|
1119
|
+
// Select the HTML content
|
|
1120
|
+
const range = document.createRange();
|
|
1121
|
+
range.selectNodeContents(tempDiv);
|
|
1122
|
+
const selection = window.getSelection();
|
|
1123
|
+
selection.removeAllRanges();
|
|
1124
|
+
selection.addRange(range);
|
|
1125
|
+
|
|
1126
|
+
// Try to copy
|
|
1127
|
+
result = document.execCommand('copy');
|
|
1128
|
+
|
|
1129
|
+
// Clear selection
|
|
1130
|
+
selection.removeAllRanges();
|
|
1131
|
+
} catch (err) {
|
|
1132
|
+
console.error('Fallback copy failed:', err);
|
|
1133
|
+
result = false;
|
|
1134
|
+
} finally {
|
|
1135
|
+
if (tempDiv && tempDiv.parentNode) {
|
|
1136
|
+
document.body.removeChild(tempDiv);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
return result;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* Convert SVG to PNG blob (based on squibview's implementation)
|
|
1145
|
+
* @param {SVGElement} svgElement - The SVG element to convert
|
|
1146
|
+
* @returns {Promise<Blob>} A promise that resolves with the PNG blob
|
|
1147
|
+
*/
|
|
1148
|
+
async function svgToPng(svgElement, needsWhiteBackground = false) {
|
|
1149
|
+
return new Promise((resolve, reject) => {
|
|
1150
|
+
const svgString = new XMLSerializer().serializeToString(svgElement);
|
|
1151
|
+
const canvas = document.createElement('canvas');
|
|
1152
|
+
const ctx = canvas.getContext('2d');
|
|
1153
|
+
const img = new Image();
|
|
1154
|
+
|
|
1155
|
+
const scale = 2;
|
|
1156
|
+
|
|
1157
|
+
// Check if this is a Mermaid-generated SVG (they don't have explicit width/height attributes)
|
|
1158
|
+
const isMermaidSvg = svgElement.closest('.mermaid') || svgElement.classList.contains('mermaid');
|
|
1159
|
+
const hasExplicitDimensions = svgElement.getAttribute('width') && svgElement.getAttribute('height');
|
|
1160
|
+
|
|
1161
|
+
let svgWidth, svgHeight;
|
|
1162
|
+
|
|
1163
|
+
if (isMermaidSvg || !hasExplicitDimensions) {
|
|
1164
|
+
// For Mermaid or other generated SVGs, prioritize computed dimensions
|
|
1165
|
+
svgWidth = svgElement.clientWidth ||
|
|
1166
|
+
(svgElement.viewBox && svgElement.viewBox.baseVal.width) ||
|
|
1167
|
+
parseFloat(svgElement.getAttribute('width')) || 400;
|
|
1168
|
+
svgHeight = svgElement.clientHeight ||
|
|
1169
|
+
(svgElement.viewBox && svgElement.viewBox.baseVal.height) ||
|
|
1170
|
+
parseFloat(svgElement.getAttribute('height')) || 300;
|
|
1171
|
+
} else {
|
|
1172
|
+
// For explicit SVGs (like fenced SVG blocks), prioritize explicit attributes
|
|
1173
|
+
svgWidth = parseFloat(svgElement.getAttribute('width')) ||
|
|
1174
|
+
(svgElement.viewBox && svgElement.viewBox.baseVal.width) ||
|
|
1175
|
+
svgElement.clientWidth || 400;
|
|
1176
|
+
svgHeight = parseFloat(svgElement.getAttribute('height')) ||
|
|
1177
|
+
(svgElement.viewBox && svgElement.viewBox.baseVal.height) ||
|
|
1178
|
+
svgElement.clientHeight || 300;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
// Ensure the SVG string has explicit dimensions by modifying it if necessary
|
|
1182
|
+
let modifiedSvgString = svgString;
|
|
1183
|
+
if (svgWidth && svgHeight) {
|
|
1184
|
+
// Create a temporary SVG element to modify the serialized string
|
|
1185
|
+
const tempDiv = document.createElement('div');
|
|
1186
|
+
tempDiv.innerHTML = svgString;
|
|
1187
|
+
const tempSvg = tempDiv.querySelector('svg');
|
|
1188
|
+
if (tempSvg) {
|
|
1189
|
+
tempSvg.setAttribute('width', svgWidth.toString());
|
|
1190
|
+
tempSvg.setAttribute('height', svgHeight.toString());
|
|
1191
|
+
modifiedSvgString = new XMLSerializer().serializeToString(tempSvg);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
canvas.width = svgWidth * scale;
|
|
1196
|
+
canvas.height = svgHeight * scale;
|
|
1197
|
+
ctx.scale(scale, scale);
|
|
1198
|
+
|
|
1199
|
+
img.onload = () => {
|
|
1200
|
+
try {
|
|
1201
|
+
// Add white background for math equations (they often have transparent backgrounds)
|
|
1202
|
+
if (needsWhiteBackground) {
|
|
1203
|
+
ctx.fillStyle = 'white';
|
|
1204
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
ctx.drawImage(img, 0, 0, svgWidth, svgHeight);
|
|
1208
|
+
canvas.toBlob(blob => {
|
|
1209
|
+
resolve(blob);
|
|
1210
|
+
}, 'image/png', 1.0);
|
|
1211
|
+
} catch (err) {
|
|
1212
|
+
reject(err);
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
|
|
1216
|
+
img.onerror = reject;
|
|
1217
|
+
// Use data URI instead of blob URL to avoid tainting the canvas
|
|
1218
|
+
const svgDataUrl = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(modifiedSvgString)}`;
|
|
1219
|
+
img.src = svgDataUrl;
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Rasterize a GeoJSON Leaflet map to PNG data URL (following Gem's guide)
|
|
1225
|
+
* @param {HTMLElement} liveContainer - The live map container element
|
|
1226
|
+
* @returns {Promise<string|null>} PNG data URL or null if failed
|
|
1227
|
+
*/
|
|
1228
|
+
async function rasterizeGeoJSONMap(liveContainer) {
|
|
1229
|
+
try {
|
|
1230
|
+
const map = liveContainer._map;
|
|
1231
|
+
if (!map) {
|
|
1232
|
+
console.warn('No map found on container');
|
|
1233
|
+
return null;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
// Get container dimensions
|
|
1237
|
+
const mapRect = liveContainer.getBoundingClientRect();
|
|
1238
|
+
const width = Math.round(mapRect.width);
|
|
1239
|
+
const height = Math.round(mapRect.height);
|
|
1240
|
+
|
|
1241
|
+
if (width === 0 || height === 0) {
|
|
1242
|
+
console.warn('Map container has zero dimensions');
|
|
1243
|
+
return null;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// Create canvas sized to the map container
|
|
1247
|
+
const canvas = document.createElement('canvas');
|
|
1248
|
+
const dpr = window.devicePixelRatio || 1;
|
|
1249
|
+
|
|
1250
|
+
// Set canvas size with DPR for sharpness
|
|
1251
|
+
canvas.width = width * dpr;
|
|
1252
|
+
canvas.height = height * dpr;
|
|
1253
|
+
canvas.style.width = width + 'px';
|
|
1254
|
+
canvas.style.height = height + 'px';
|
|
1255
|
+
|
|
1256
|
+
const ctx = canvas.getContext('2d');
|
|
1257
|
+
ctx.scale(dpr, dpr);
|
|
1258
|
+
|
|
1259
|
+
// White background
|
|
1260
|
+
ctx.fillStyle = '#FFFFFF';
|
|
1261
|
+
ctx.fillRect(0, 0, width, height);
|
|
1262
|
+
|
|
1263
|
+
// 1. Draw tiles from THIS container only
|
|
1264
|
+
const tiles = liveContainer.querySelectorAll('.leaflet-tile');
|
|
1265
|
+
|
|
1266
|
+
const tilePromises = [];
|
|
1267
|
+
for (const tile of tiles) {
|
|
1268
|
+
tilePromises.push(new Promise((resolve) => {
|
|
1269
|
+
const img = new Image();
|
|
1270
|
+
img.crossOrigin = 'anonymous';
|
|
1271
|
+
|
|
1272
|
+
img.onload = () => {
|
|
1273
|
+
try {
|
|
1274
|
+
// Calculate tile position relative to container
|
|
1275
|
+
const tileRect = tile.getBoundingClientRect();
|
|
1276
|
+
const offsetX = tileRect.left - mapRect.left;
|
|
1277
|
+
const offsetY = tileRect.top - mapRect.top;
|
|
1278
|
+
|
|
1279
|
+
// Draw tile at correct position
|
|
1280
|
+
ctx.drawImage(img, offsetX, offsetY, tileRect.width, tileRect.height);
|
|
1281
|
+
} catch (err) {
|
|
1282
|
+
console.warn('Failed to draw tile:', err);
|
|
1283
|
+
}
|
|
1284
|
+
resolve();
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
img.onerror = () => {
|
|
1288
|
+
console.warn('Failed to load tile:', tile.src);
|
|
1289
|
+
resolve();
|
|
1290
|
+
};
|
|
1291
|
+
|
|
1292
|
+
img.src = tile.src;
|
|
1293
|
+
}));
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
// Wait for all tiles to load
|
|
1297
|
+
await Promise.all(tilePromises);
|
|
1298
|
+
|
|
1299
|
+
// 2. Draw vector overlays (SVG paths for GeoJSON features)
|
|
1300
|
+
const svgOverlays = liveContainer.querySelectorAll('svg:not(.leaflet-attribution-flag)');
|
|
1301
|
+
|
|
1302
|
+
for (const svg of svgOverlays) {
|
|
1303
|
+
// Skip attribution/control overlays
|
|
1304
|
+
if (svg.closest('.leaflet-control')) continue;
|
|
1305
|
+
|
|
1306
|
+
try {
|
|
1307
|
+
const svgRect = svg.getBoundingClientRect();
|
|
1308
|
+
const offsetX = svgRect.left - mapRect.left;
|
|
1309
|
+
const offsetY = svgRect.top - mapRect.top;
|
|
1310
|
+
|
|
1311
|
+
// Serialize SVG
|
|
1312
|
+
const serializer = new XMLSerializer();
|
|
1313
|
+
const svgStr = serializer.serializeToString(svg);
|
|
1314
|
+
const svgBlob = new Blob([svgStr], { type: 'image/svg+xml;charset=utf-8' });
|
|
1315
|
+
const url = URL.createObjectURL(svgBlob);
|
|
1316
|
+
|
|
1317
|
+
// Draw SVG overlay
|
|
1318
|
+
await new Promise((resolve, reject) => {
|
|
1319
|
+
const img = new Image();
|
|
1320
|
+
img.onload = () => {
|
|
1321
|
+
ctx.drawImage(img, offsetX, offsetY, svgRect.width, svgRect.height);
|
|
1322
|
+
URL.revokeObjectURL(url);
|
|
1323
|
+
resolve();
|
|
1324
|
+
};
|
|
1325
|
+
img.onerror = () => {
|
|
1326
|
+
URL.revokeObjectURL(url);
|
|
1327
|
+
reject(new Error('Failed to load SVG overlay'));
|
|
1328
|
+
};
|
|
1329
|
+
img.src = url;
|
|
1330
|
+
});
|
|
1331
|
+
} catch (err) {
|
|
1332
|
+
console.warn('Failed to draw SVG overlay:', err);
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// 3. Draw marker icons if any
|
|
1337
|
+
const markerIcons = liveContainer.querySelectorAll('.leaflet-marker-icon');
|
|
1338
|
+
|
|
1339
|
+
for (const marker of markerIcons) {
|
|
1340
|
+
try {
|
|
1341
|
+
const img = new Image();
|
|
1342
|
+
img.crossOrigin = 'anonymous';
|
|
1343
|
+
|
|
1344
|
+
await new Promise((resolve) => {
|
|
1345
|
+
img.onload = () => {
|
|
1346
|
+
const markerRect = marker.getBoundingClientRect();
|
|
1347
|
+
const offsetX = markerRect.left - mapRect.left;
|
|
1348
|
+
const offsetY = markerRect.top - mapRect.top;
|
|
1349
|
+
ctx.drawImage(img, offsetX, offsetY, markerRect.width, markerRect.height);
|
|
1350
|
+
resolve();
|
|
1351
|
+
};
|
|
1352
|
+
img.onerror = resolve;
|
|
1353
|
+
img.src = marker.src;
|
|
1354
|
+
});
|
|
1355
|
+
} catch (err) {
|
|
1356
|
+
console.warn('Failed to draw marker icon:', err);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
// Return PNG data URL
|
|
1361
|
+
return canvas.toDataURL('image/png', 1.0);
|
|
1362
|
+
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
console.error('Failed to rasterize GeoJSON map:', error);
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Get rendered content as rich HTML suitable for clipboard
|
|
1371
|
+
* @param {HTMLElement} previewPanel - The preview panel element to copy from
|
|
1372
|
+
* @returns {Promise<{success: boolean, html?: string, text?: string}>}
|
|
1373
|
+
*/
|
|
1374
|
+
async function getRenderedContent(previewPanel) {
|
|
1375
|
+
if (!previewPanel) {
|
|
1376
|
+
throw new Error('No preview panel available');
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// Check if MathJax needs to render (only if not already rendered)
|
|
1380
|
+
const mathBlocks = previewPanel.querySelectorAll('.math-display');
|
|
1381
|
+
if (mathBlocks.length > 0) {
|
|
1382
|
+
// Check if already rendered (has mjx-container inside)
|
|
1383
|
+
const needsRendering = Array.from(mathBlocks).some(block => !block.querySelector('mjx-container'));
|
|
1384
|
+
|
|
1385
|
+
if (needsRendering && window.MathJax && window.MathJax.typesetPromise) {
|
|
1386
|
+
try {
|
|
1387
|
+
await window.MathJax.typesetPromise(Array.from(mathBlocks));
|
|
1388
|
+
} catch (err) {
|
|
1389
|
+
console.warn('MathJax typesetting failed:', err);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Clone the preview panel to avoid modifying the actual DOM
|
|
1395
|
+
const clone = previewPanel.cloneNode(true);
|
|
1396
|
+
|
|
1397
|
+
// Process different fence types for rich copy
|
|
1398
|
+
try {
|
|
1399
|
+
// Phase 1: Process basic markdown elements with inline styles
|
|
1400
|
+
|
|
1401
|
+
// 1.1 Text formatting - add inline styles
|
|
1402
|
+
clone.querySelectorAll('strong, b').forEach(el => {
|
|
1403
|
+
el.style.fontWeight = 'bold';
|
|
1404
|
+
});
|
|
1405
|
+
|
|
1406
|
+
clone.querySelectorAll('em, i').forEach(el => {
|
|
1407
|
+
el.style.fontStyle = 'italic';
|
|
1408
|
+
});
|
|
1409
|
+
|
|
1410
|
+
clone.querySelectorAll('del, s, strike').forEach(el => {
|
|
1411
|
+
el.style.textDecoration = 'line-through';
|
|
1412
|
+
});
|
|
1413
|
+
|
|
1414
|
+
clone.querySelectorAll('u').forEach(el => {
|
|
1415
|
+
el.style.textDecoration = 'underline';
|
|
1416
|
+
});
|
|
1417
|
+
|
|
1418
|
+
clone.querySelectorAll('code:not(pre code)').forEach(el => {
|
|
1419
|
+
el.style.backgroundColor = '#f4f4f4';
|
|
1420
|
+
el.style.padding = '2px 4px';
|
|
1421
|
+
el.style.borderRadius = '3px';
|
|
1422
|
+
el.style.fontFamily = 'monospace';
|
|
1423
|
+
el.style.fontSize = '0.9em';
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
// 1.2 Block elements - add inline styles
|
|
1427
|
+
clone.querySelectorAll('h1').forEach(el => {
|
|
1428
|
+
el.style.fontSize = '2em';
|
|
1429
|
+
el.style.fontWeight = 'bold';
|
|
1430
|
+
el.style.marginTop = '0.67em';
|
|
1431
|
+
el.style.marginBottom = '0.67em';
|
|
1432
|
+
});
|
|
1433
|
+
|
|
1434
|
+
clone.querySelectorAll('h2').forEach(el => {
|
|
1435
|
+
el.style.fontSize = '1.5em';
|
|
1436
|
+
el.style.fontWeight = 'bold';
|
|
1437
|
+
el.style.marginTop = '0.83em';
|
|
1438
|
+
el.style.marginBottom = '0.83em';
|
|
1439
|
+
});
|
|
1440
|
+
|
|
1441
|
+
clone.querySelectorAll('h3').forEach(el => {
|
|
1442
|
+
el.style.fontSize = '1.17em';
|
|
1443
|
+
el.style.fontWeight = 'bold';
|
|
1444
|
+
el.style.marginTop = '1em';
|
|
1445
|
+
el.style.marginBottom = '1em';
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
clone.querySelectorAll('h4').forEach(el => {
|
|
1449
|
+
el.style.fontSize = '1em';
|
|
1450
|
+
el.style.fontWeight = 'bold';
|
|
1451
|
+
el.style.marginTop = '1.33em';
|
|
1452
|
+
el.style.marginBottom = '1.33em';
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
clone.querySelectorAll('h5').forEach(el => {
|
|
1456
|
+
el.style.fontSize = '0.83em';
|
|
1457
|
+
el.style.fontWeight = 'bold';
|
|
1458
|
+
el.style.marginTop = '1.67em';
|
|
1459
|
+
el.style.marginBottom = '1.67em';
|
|
1460
|
+
});
|
|
1461
|
+
|
|
1462
|
+
clone.querySelectorAll('h6').forEach(el => {
|
|
1463
|
+
el.style.fontSize = '0.67em';
|
|
1464
|
+
el.style.fontWeight = 'bold';
|
|
1465
|
+
el.style.marginTop = '2.33em';
|
|
1466
|
+
el.style.marginBottom = '2.33em';
|
|
1467
|
+
});
|
|
1468
|
+
|
|
1469
|
+
clone.querySelectorAll('blockquote').forEach(el => {
|
|
1470
|
+
el.style.borderLeft = '4px solid #ddd';
|
|
1471
|
+
el.style.marginLeft = '0';
|
|
1472
|
+
el.style.paddingLeft = '1em';
|
|
1473
|
+
el.style.color = '#666';
|
|
1474
|
+
});
|
|
1475
|
+
|
|
1476
|
+
clone.querySelectorAll('hr').forEach(el => {
|
|
1477
|
+
el.style.border = 'none';
|
|
1478
|
+
el.style.borderTop = '1px solid #ccc';
|
|
1479
|
+
el.style.margin = '1em 0';
|
|
1480
|
+
});
|
|
1481
|
+
|
|
1482
|
+
// 1.3 Tables - add inline styles
|
|
1483
|
+
clone.querySelectorAll('table').forEach(table => {
|
|
1484
|
+
table.style.borderCollapse = 'collapse';
|
|
1485
|
+
table.style.width = '100%';
|
|
1486
|
+
table.style.marginBottom = '1em';
|
|
1487
|
+
});
|
|
1488
|
+
|
|
1489
|
+
clone.querySelectorAll('th').forEach(th => {
|
|
1490
|
+
th.style.border = '1px solid #ccc';
|
|
1491
|
+
th.style.padding = '8px';
|
|
1492
|
+
th.style.textAlign = 'left';
|
|
1493
|
+
th.style.backgroundColor = '#f0f0f0';
|
|
1494
|
+
th.style.fontWeight = 'bold';
|
|
1495
|
+
});
|
|
1496
|
+
|
|
1497
|
+
clone.querySelectorAll('td').forEach(td => {
|
|
1498
|
+
td.style.border = '1px solid #ccc';
|
|
1499
|
+
td.style.padding = '8px';
|
|
1500
|
+
td.style.textAlign = 'left';
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
// 1.4 Links - add inline styles
|
|
1504
|
+
clone.querySelectorAll('a').forEach(a => {
|
|
1505
|
+
a.style.color = '#0066cc';
|
|
1506
|
+
a.style.textDecoration = 'underline';
|
|
1507
|
+
});
|
|
1508
|
+
|
|
1509
|
+
// Process code blocks - wrap in table and add syntax highlighting colors
|
|
1510
|
+
clone.querySelectorAll('pre code').forEach(block => {
|
|
1511
|
+
const pre = block.parentElement;
|
|
1512
|
+
|
|
1513
|
+
// Add inline styles for syntax highlighting (GitHub theme colors)
|
|
1514
|
+
if (block.classList.contains('hljs')) {
|
|
1515
|
+
// Apply inline styles to all highlight.js elements
|
|
1516
|
+
block.querySelectorAll('.hljs-keyword').forEach(el => {
|
|
1517
|
+
el.style.color = '#d73a49';
|
|
1518
|
+
el.style.fontWeight = 'bold';
|
|
1519
|
+
});
|
|
1520
|
+
block.querySelectorAll('.hljs-string').forEach(el => {
|
|
1521
|
+
el.style.color = '#032f62';
|
|
1522
|
+
});
|
|
1523
|
+
block.querySelectorAll('.hljs-number').forEach(el => {
|
|
1524
|
+
el.style.color = '#005cc5';
|
|
1525
|
+
});
|
|
1526
|
+
block.querySelectorAll('.hljs-comment').forEach(el => {
|
|
1527
|
+
el.style.color = '#6a737d';
|
|
1528
|
+
el.style.fontStyle = 'italic';
|
|
1529
|
+
});
|
|
1530
|
+
block.querySelectorAll('.hljs-function').forEach(el => {
|
|
1531
|
+
el.style.color = '#6f42c1';
|
|
1532
|
+
});
|
|
1533
|
+
block.querySelectorAll('.hljs-class').forEach(el => {
|
|
1534
|
+
el.style.color = '#6f42c1';
|
|
1535
|
+
});
|
|
1536
|
+
block.querySelectorAll('.hljs-title').forEach(el => {
|
|
1537
|
+
el.style.color = '#6f42c1';
|
|
1538
|
+
});
|
|
1539
|
+
block.querySelectorAll('.hljs-built_in').forEach(el => {
|
|
1540
|
+
el.style.color = '#005cc5';
|
|
1541
|
+
});
|
|
1542
|
+
block.querySelectorAll('.hljs-literal').forEach(el => {
|
|
1543
|
+
el.style.color = '#005cc5';
|
|
1544
|
+
});
|
|
1545
|
+
block.querySelectorAll('.hljs-meta').forEach(el => {
|
|
1546
|
+
el.style.color = '#005cc5';
|
|
1547
|
+
});
|
|
1548
|
+
block.querySelectorAll('.hljs-attr').forEach(el => {
|
|
1549
|
+
el.style.color = '#22863a';
|
|
1550
|
+
});
|
|
1551
|
+
block.querySelectorAll('.hljs-variable').forEach(el => {
|
|
1552
|
+
el.style.color = '#e36209';
|
|
1553
|
+
});
|
|
1554
|
+
block.querySelectorAll('.hljs-regexp').forEach(el => {
|
|
1555
|
+
el.style.color = '#032f62';
|
|
1556
|
+
});
|
|
1557
|
+
block.querySelectorAll('.hljs-selector-class').forEach(el => {
|
|
1558
|
+
el.style.color = '#22863a';
|
|
1559
|
+
});
|
|
1560
|
+
block.querySelectorAll('.hljs-selector-id').forEach(el => {
|
|
1561
|
+
el.style.color = '#6f42c1';
|
|
1562
|
+
});
|
|
1563
|
+
block.querySelectorAll('.hljs-selector-tag').forEach(el => {
|
|
1564
|
+
el.style.color = '#22863a';
|
|
1565
|
+
});
|
|
1566
|
+
block.querySelectorAll('.hljs-tag').forEach(el => {
|
|
1567
|
+
el.style.color = '#22863a';
|
|
1568
|
+
});
|
|
1569
|
+
block.querySelectorAll('.hljs-name').forEach(el => {
|
|
1570
|
+
el.style.color = '#22863a';
|
|
1571
|
+
});
|
|
1572
|
+
block.querySelectorAll('.hljs-attribute').forEach(el => {
|
|
1573
|
+
el.style.color = '#6f42c1';
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
const table = document.createElement('table');
|
|
1578
|
+
table.style.width = '100%';
|
|
1579
|
+
table.style.borderCollapse = 'collapse';
|
|
1580
|
+
table.style.border = 'none';
|
|
1581
|
+
table.style.marginBottom = '1em';
|
|
1582
|
+
|
|
1583
|
+
const tr = document.createElement('tr');
|
|
1584
|
+
const td = document.createElement('td');
|
|
1585
|
+
td.style.backgroundColor = '#f7f7f7';
|
|
1586
|
+
td.style.padding = '12px';
|
|
1587
|
+
td.style.fontFamily = 'Consolas, Monaco, "Courier New", monospace';
|
|
1588
|
+
td.style.fontSize = '14px';
|
|
1589
|
+
td.style.lineHeight = '1.4';
|
|
1590
|
+
td.style.whiteSpace = 'pre';
|
|
1591
|
+
td.style.overflowX = 'auto';
|
|
1592
|
+
td.style.border = '1px solid #ddd';
|
|
1593
|
+
td.style.borderRadius = '4px';
|
|
1594
|
+
|
|
1595
|
+
// Move the formatted code content with inline styles
|
|
1596
|
+
td.innerHTML = block.innerHTML;
|
|
1597
|
+
|
|
1598
|
+
tr.appendChild(td);
|
|
1599
|
+
table.appendChild(tr);
|
|
1600
|
+
|
|
1601
|
+
// Replace the pre element with the table
|
|
1602
|
+
pre.parentNode.replaceChild(table, pre);
|
|
1603
|
+
});
|
|
1604
|
+
|
|
1605
|
+
// Process images - convert to data URLs and ensure proper dimensions
|
|
1606
|
+
const images = clone.querySelectorAll('img');
|
|
1607
|
+
for (const img of images) {
|
|
1608
|
+
// Ensure image has dimensions for Google Docs compatibility
|
|
1609
|
+
if (!img.width && img.naturalWidth) {
|
|
1610
|
+
img.width = img.naturalWidth;
|
|
1611
|
+
}
|
|
1612
|
+
if (!img.height && img.naturalHeight) {
|
|
1613
|
+
img.height = img.naturalHeight;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
// Set max dimensions to prevent huge images
|
|
1617
|
+
const maxWidth = 800;
|
|
1618
|
+
const maxHeight = 600;
|
|
1619
|
+
if (img.width > maxWidth || img.height > maxHeight) {
|
|
1620
|
+
const scale = Math.min(maxWidth / img.width, maxHeight / img.height);
|
|
1621
|
+
img.width = Math.round(img.width * scale);
|
|
1622
|
+
img.height = Math.round(img.height * scale);
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
// Ensure width and height attributes are set
|
|
1626
|
+
if (img.width) {
|
|
1627
|
+
img.setAttribute('width', img.width.toString());
|
|
1628
|
+
img.style.width = img.width + 'px';
|
|
1629
|
+
}
|
|
1630
|
+
if (img.height) {
|
|
1631
|
+
img.setAttribute('height', img.height.toString());
|
|
1632
|
+
img.style.height = img.height + 'px';
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
// Add v:shapes for Word compatibility
|
|
1636
|
+
if (!img.getAttribute('v:shapes')) {
|
|
1637
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
// Skip if already a data URL
|
|
1641
|
+
if (img.src && !img.src.startsWith('data:')) {
|
|
1642
|
+
try {
|
|
1643
|
+
// Try to convert to data URL
|
|
1644
|
+
const response = await fetch(img.src);
|
|
1645
|
+
const blob = await response.blob();
|
|
1646
|
+
|
|
1647
|
+
// Check if image is too large (Google Docs has limits)
|
|
1648
|
+
const maxSize = 2 * 1024 * 1024; // 2MB limit for inline images
|
|
1649
|
+
if (blob.size > maxSize) {
|
|
1650
|
+
console.warn('Image too large for inline data URL:', img.src, 'Size:', blob.size);
|
|
1651
|
+
// For large images, we might want to resize or keep the URL
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
const dataUrl = await new Promise(resolve => {
|
|
1656
|
+
const reader = new FileReader();
|
|
1657
|
+
reader.onloadend = () => resolve(reader.result);
|
|
1658
|
+
reader.readAsDataURL(blob);
|
|
1659
|
+
});
|
|
1660
|
+
img.src = dataUrl;
|
|
1661
|
+
} catch (err) {
|
|
1662
|
+
console.warn('Failed to convert image to data URL:', img.src, err);
|
|
1663
|
+
// Keep original src if conversion fails
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// Phase 2: Process fence block types
|
|
1669
|
+
// 1. Process STL 3D models - convert canvas to image or placeholder
|
|
1670
|
+
const stlContainers = clone.querySelectorAll('.qde-stl-container');
|
|
1671
|
+
for (const container of stlContainers) {
|
|
1672
|
+
try {
|
|
1673
|
+
// Find the corresponding original container to get the canvas
|
|
1674
|
+
const containerId = container.dataset.stlId;
|
|
1675
|
+
const originalContainer = previewPanel.querySelector(`.qde-stl-container[data-stl-id="${containerId}"]`);
|
|
1676
|
+
|
|
1677
|
+
if (originalContainer) {
|
|
1678
|
+
// Look for canvas element in the original container (Three.js WebGL canvas)
|
|
1679
|
+
const canvas = originalContainer.querySelector('canvas');
|
|
1680
|
+
if (canvas && canvas.width > 0 && canvas.height > 0) {
|
|
1681
|
+
try {
|
|
1682
|
+
// Get Three.js references stored on the container (like squibview)
|
|
1683
|
+
const renderer = originalContainer._threeRenderer;
|
|
1684
|
+
const scene = originalContainer._threeScene;
|
|
1685
|
+
const camera = originalContainer._threeCamera;
|
|
1686
|
+
|
|
1687
|
+
// If we have access to the Three.js objects, force render the scene
|
|
1688
|
+
if (renderer && scene && camera) {
|
|
1689
|
+
renderer.render(scene, camera);
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
// Try to capture the canvas as an image
|
|
1693
|
+
const dataUrl = canvas.toDataURL('image/png', 1.0);
|
|
1694
|
+
const img = document.createElement('img');
|
|
1695
|
+
img.src = dataUrl;
|
|
1696
|
+
|
|
1697
|
+
// Use canvas dimensions for the image
|
|
1698
|
+
const imgWidth = canvas.width / 2; // Divide by scale factor (2x for retina)
|
|
1699
|
+
const imgHeight = canvas.height / 2;
|
|
1700
|
+
|
|
1701
|
+
// Set both HTML attributes and CSS properties for maximum compatibility
|
|
1702
|
+
img.width = imgWidth;
|
|
1703
|
+
img.height = imgHeight;
|
|
1704
|
+
img.setAttribute('width', imgWidth.toString());
|
|
1705
|
+
img.setAttribute('height', imgHeight.toString());
|
|
1706
|
+
img.style.width = imgWidth + 'px';
|
|
1707
|
+
img.style.height = imgHeight + 'px';
|
|
1708
|
+
img.style.maxWidth = 'none';
|
|
1709
|
+
img.style.maxHeight = 'none';
|
|
1710
|
+
img.style.border = '1px solid #ddd';
|
|
1711
|
+
img.style.borderRadius = '4px';
|
|
1712
|
+
img.style.margin = '0.5em 0';
|
|
1713
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
1714
|
+
img.alt = 'STL 3D Model';
|
|
1715
|
+
|
|
1716
|
+
container.parentNode.replaceChild(img, container);
|
|
1717
|
+
continue;
|
|
1718
|
+
} catch (canvasErr) {
|
|
1719
|
+
console.warn('Failed to convert STL canvas to image (likely WebGL context issue):', canvasErr);
|
|
1720
|
+
}
|
|
1721
|
+
} else {
|
|
1722
|
+
console.warn('No valid canvas found in STL container');
|
|
1723
|
+
}
|
|
1724
|
+
} else {
|
|
1725
|
+
console.warn('Could not find original STL container');
|
|
1726
|
+
}
|
|
1727
|
+
} catch (err) {
|
|
1728
|
+
console.error('Error processing STL container for copy:', err);
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
// Fallback to placeholder if canvas conversion fails
|
|
1732
|
+
const placeholder = document.createElement('div');
|
|
1733
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
1734
|
+
placeholder.textContent = '[STL 3D Model - Interactive content not available in copy]';
|
|
1735
|
+
container.parentNode.replaceChild(placeholder, container);
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
// 2. Process Mermaid diagrams - convert SVG to PNG
|
|
1739
|
+
const mermaidContainers = clone.querySelectorAll('.mermaid');
|
|
1740
|
+
for (const container of mermaidContainers) {
|
|
1741
|
+
const svg = container.querySelector('svg');
|
|
1742
|
+
if (svg) {
|
|
1743
|
+
try {
|
|
1744
|
+
const pngBlob = await svgToPng(svg);
|
|
1745
|
+
const dataUrl = await new Promise(resolve => {
|
|
1746
|
+
const reader = new FileReader();
|
|
1747
|
+
reader.onloadend = () => resolve(reader.result);
|
|
1748
|
+
reader.readAsDataURL(pngBlob);
|
|
1749
|
+
});
|
|
1750
|
+
|
|
1751
|
+
const img = document.createElement('img');
|
|
1752
|
+
img.src = dataUrl;
|
|
1753
|
+
|
|
1754
|
+
// Use the exact same dimension calculation logic as svgToPng (like squibview)
|
|
1755
|
+
const isMermaidSvg = svg.closest('.mermaid') || svg.classList.contains('mermaid');
|
|
1756
|
+
const hasExplicitDimensions = svg.getAttribute('width') && svg.getAttribute('height');
|
|
1757
|
+
|
|
1758
|
+
let imgWidth, imgHeight;
|
|
1759
|
+
|
|
1760
|
+
if (isMermaidSvg || !hasExplicitDimensions) {
|
|
1761
|
+
// For Mermaid or other generated SVGs, prioritize computed dimensions
|
|
1762
|
+
imgWidth = svg.clientWidth ||
|
|
1763
|
+
(svg.viewBox && svg.viewBox.baseVal.width) ||
|
|
1764
|
+
parseFloat(svg.getAttribute('width')) || 400;
|
|
1765
|
+
imgHeight = svg.clientHeight ||
|
|
1766
|
+
(svg.viewBox && svg.viewBox.baseVal.height) ||
|
|
1767
|
+
parseFloat(svg.getAttribute('height')) || 300;
|
|
1768
|
+
} else {
|
|
1769
|
+
// For explicit SVGs (like fenced SVG blocks), prioritize explicit attributes
|
|
1770
|
+
imgWidth = parseFloat(svg.getAttribute('width')) ||
|
|
1771
|
+
(svg.viewBox && svg.viewBox.baseVal.width) ||
|
|
1772
|
+
svg.clientWidth || 400;
|
|
1773
|
+
imgHeight = parseFloat(svg.getAttribute('height')) ||
|
|
1774
|
+
(svg.viewBox && svg.viewBox.baseVal.height) ||
|
|
1775
|
+
svg.clientHeight || 300;
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
// Set both HTML attributes and CSS properties for maximum compatibility (like squibview)
|
|
1779
|
+
img.width = imgWidth;
|
|
1780
|
+
img.height = imgHeight;
|
|
1781
|
+
img.setAttribute('width', imgWidth.toString());
|
|
1782
|
+
img.setAttribute('height', imgHeight.toString());
|
|
1783
|
+
img.style.width = imgWidth + 'px';
|
|
1784
|
+
img.style.height = imgHeight + 'px';
|
|
1785
|
+
img.style.maxWidth = 'none'; // Prevent CSS from constraining the image
|
|
1786
|
+
img.style.maxHeight = 'none';
|
|
1787
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
1788
|
+
img.alt = 'Mermaid Diagram';
|
|
1789
|
+
|
|
1790
|
+
container.parentNode.replaceChild(img, container);
|
|
1791
|
+
} catch (err) {
|
|
1792
|
+
console.warn('Failed to convert Mermaid diagram:', err);
|
|
1793
|
+
// Fallback: leave as SVG
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// 3. Process Chart.js charts - convert canvas to image
|
|
1799
|
+
const chartContainers = clone.querySelectorAll('.qde-chart-container');
|
|
1800
|
+
for (const container of chartContainers) {
|
|
1801
|
+
try {
|
|
1802
|
+
const containerId = container.dataset.chartId;
|
|
1803
|
+
const originalContainer = previewPanel.querySelector(`.qde-chart-container[data-chart-id="${containerId}"]`);
|
|
1804
|
+
|
|
1805
|
+
if (originalContainer) {
|
|
1806
|
+
const canvas = originalContainer.querySelector('canvas');
|
|
1807
|
+
if (canvas && canvas.width > 0 && canvas.height > 0) {
|
|
1808
|
+
try {
|
|
1809
|
+
const dataUrl = canvas.toDataURL('image/png', 1.0);
|
|
1810
|
+
const img = document.createElement('img');
|
|
1811
|
+
img.src = dataUrl;
|
|
1812
|
+
|
|
1813
|
+
// Use canvas dimensions for the image
|
|
1814
|
+
const imgWidth = canvas.width;
|
|
1815
|
+
const imgHeight = canvas.height;
|
|
1816
|
+
|
|
1817
|
+
// Set both HTML attributes and CSS properties for maximum compatibility
|
|
1818
|
+
img.width = imgWidth;
|
|
1819
|
+
img.height = imgHeight;
|
|
1820
|
+
img.setAttribute('width', imgWidth.toString());
|
|
1821
|
+
img.setAttribute('height', imgHeight.toString());
|
|
1822
|
+
img.style.width = imgWidth + 'px';
|
|
1823
|
+
img.style.height = imgHeight + 'px';
|
|
1824
|
+
img.style.maxWidth = 'none';
|
|
1825
|
+
img.style.maxHeight = 'none';
|
|
1826
|
+
img.style.margin = '0.5em 0';
|
|
1827
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
1828
|
+
img.alt = 'Chart';
|
|
1829
|
+
|
|
1830
|
+
container.parentNode.replaceChild(img, container);
|
|
1831
|
+
continue;
|
|
1832
|
+
} catch (canvasErr) {
|
|
1833
|
+
console.warn('Failed to convert chart canvas to image:', canvasErr);
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
} catch (err) {
|
|
1838
|
+
console.warn('Error processing chart container:', err);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// Fallback to placeholder
|
|
1842
|
+
const placeholder = document.createElement('div');
|
|
1843
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
1844
|
+
placeholder.textContent = '[Chart - Interactive content not available in copy]';
|
|
1845
|
+
container.parentNode.replaceChild(placeholder, container);
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
// 4. Process SVG fenced blocks - convert to PNG
|
|
1849
|
+
const svgContainers = clone.querySelectorAll('.qde-svg-container svg');
|
|
1850
|
+
for (const svg of svgContainers) {
|
|
1851
|
+
try {
|
|
1852
|
+
const pngBlob = await svgToPng(svg);
|
|
1853
|
+
const dataUrl = await new Promise(resolve => {
|
|
1854
|
+
const reader = new FileReader();
|
|
1855
|
+
reader.onloadend = () => resolve(reader.result);
|
|
1856
|
+
reader.readAsDataURL(pngBlob);
|
|
1857
|
+
});
|
|
1858
|
+
|
|
1859
|
+
const img = document.createElement('img');
|
|
1860
|
+
img.src = dataUrl;
|
|
1861
|
+
|
|
1862
|
+
// Calculate dimensions for the SVG
|
|
1863
|
+
const hasExplicitDimensions = svg.getAttribute('width') && svg.getAttribute('height');
|
|
1864
|
+
|
|
1865
|
+
let imgWidth, imgHeight;
|
|
1866
|
+
|
|
1867
|
+
if (hasExplicitDimensions) {
|
|
1868
|
+
// For explicit SVGs (like fenced SVG blocks), prioritize explicit attributes
|
|
1869
|
+
imgWidth = parseFloat(svg.getAttribute('width')) ||
|
|
1870
|
+
(svg.viewBox && svg.viewBox.baseVal.width) ||
|
|
1871
|
+
svg.clientWidth || 400;
|
|
1872
|
+
imgHeight = parseFloat(svg.getAttribute('height')) ||
|
|
1873
|
+
(svg.viewBox && svg.viewBox.baseVal.height) ||
|
|
1874
|
+
svg.clientHeight || 300;
|
|
1875
|
+
} else {
|
|
1876
|
+
// For generated SVGs, prioritize computed dimensions
|
|
1877
|
+
imgWidth = svg.clientWidth ||
|
|
1878
|
+
(svg.viewBox && svg.viewBox.baseVal.width) ||
|
|
1879
|
+
parseFloat(svg.getAttribute('width')) || 400;
|
|
1880
|
+
imgHeight = svg.clientHeight ||
|
|
1881
|
+
(svg.viewBox && svg.viewBox.baseVal.height) ||
|
|
1882
|
+
parseFloat(svg.getAttribute('height')) || 300;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// Set both HTML attributes and CSS properties for maximum compatibility
|
|
1886
|
+
img.width = imgWidth;
|
|
1887
|
+
img.height = imgHeight;
|
|
1888
|
+
img.setAttribute('width', imgWidth.toString());
|
|
1889
|
+
img.setAttribute('height', imgHeight.toString());
|
|
1890
|
+
img.style.width = imgWidth + 'px';
|
|
1891
|
+
img.style.height = imgHeight + 'px';
|
|
1892
|
+
img.style.maxWidth = 'none'; // Prevent CSS from constraining the image
|
|
1893
|
+
img.style.maxHeight = 'none';
|
|
1894
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
1895
|
+
img.alt = 'SVG Image';
|
|
1896
|
+
|
|
1897
|
+
svg.parentNode.replaceChild(img, svg);
|
|
1898
|
+
} catch (err) {
|
|
1899
|
+
console.warn('Failed to convert SVG to image:', err);
|
|
1900
|
+
// Leave as SVG if conversion fails
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
// 5. Process Math equations - convert to PNG images (exactly like SquibView)
|
|
1905
|
+
const mathElements = Array.from(clone.querySelectorAll('.math-display'));
|
|
1906
|
+
|
|
1907
|
+
if (mathElements.length > 0) {
|
|
1908
|
+
for (const mathEl of mathElements) {
|
|
1909
|
+
try {
|
|
1910
|
+
// Find SVG inside the math element (MathJax creates it)
|
|
1911
|
+
const svg = mathEl.querySelector('svg');
|
|
1912
|
+
if (!svg) {
|
|
1913
|
+
console.warn('No SVG found in math element, skipping');
|
|
1914
|
+
continue;
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
// Convert SVG to PNG data URL (exactly like SquibView)
|
|
1918
|
+
const serializer = new XMLSerializer();
|
|
1919
|
+
const svgStr = serializer.serializeToString(svg);
|
|
1920
|
+
const svgBlob = new Blob([svgStr], { type: "image/svg+xml;charset=utf-8" });
|
|
1921
|
+
const url = URL.createObjectURL(svgBlob);
|
|
1922
|
+
|
|
1923
|
+
const img = new Image();
|
|
1924
|
+
const dataUrl = await new Promise((resolve, reject) => {
|
|
1925
|
+
img.onload = function () {
|
|
1926
|
+
const canvas = document.createElement('canvas');
|
|
1927
|
+
|
|
1928
|
+
// Determine SVG dimensions robustly (exactly like SquibView)
|
|
1929
|
+
let width, height;
|
|
1930
|
+
try {
|
|
1931
|
+
// First try baseVal.value (works for absolute units)
|
|
1932
|
+
width = svg.width.baseVal.value;
|
|
1933
|
+
height = svg.height.baseVal.value;
|
|
1934
|
+
} catch (e) {
|
|
1935
|
+
// Fallback for relative units - use viewBox or rendered size
|
|
1936
|
+
if (svg.viewBox && svg.viewBox.baseVal) {
|
|
1937
|
+
width = svg.viewBox.baseVal.width;
|
|
1938
|
+
height = svg.viewBox.baseVal.height;
|
|
1939
|
+
} else {
|
|
1940
|
+
// Use the natural size of the loaded image
|
|
1941
|
+
width = img.naturalWidth || img.width || 200;
|
|
1942
|
+
height = img.naturalHeight || img.height || 50;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// Scale down for much smaller paste sizes
|
|
1947
|
+
const targetMaxWidth = 150; // Further reduced
|
|
1948
|
+
const targetMaxHeight = 45; // Further reduced
|
|
1949
|
+
|
|
1950
|
+
// Apply aggressive downsizing for MathJax SVGs
|
|
1951
|
+
let scaleFactor = 0.04; // Further reduced for smaller output
|
|
1952
|
+
|
|
1953
|
+
let scaledWidth = width * scaleFactor;
|
|
1954
|
+
let scaledHeight = height * scaleFactor;
|
|
1955
|
+
|
|
1956
|
+
// If still too large after base scaling, scale down further
|
|
1957
|
+
if (scaledWidth > targetMaxWidth || scaledHeight > targetMaxHeight) {
|
|
1958
|
+
const scaleX = targetMaxWidth / scaledWidth;
|
|
1959
|
+
const scaleY = targetMaxHeight / scaledHeight;
|
|
1960
|
+
scaleFactor *= Math.min(scaleX, scaleY);
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
width *= scaleFactor;
|
|
1964
|
+
height *= scaleFactor;
|
|
1965
|
+
|
|
1966
|
+
// Use higher DPR for crisp rendering at smaller sizes
|
|
1967
|
+
const dpr = 2; // Fixed 2x for consistent quality
|
|
1968
|
+
canvas.width = width * dpr;
|
|
1969
|
+
canvas.height = height * dpr;
|
|
1970
|
+
canvas.style.width = width + 'px';
|
|
1971
|
+
canvas.style.height = height + 'px';
|
|
1972
|
+
|
|
1973
|
+
const ctx = canvas.getContext('2d');
|
|
1974
|
+
ctx.scale(dpr, dpr);
|
|
1975
|
+
|
|
1976
|
+
// White background
|
|
1977
|
+
ctx.fillStyle = "#FFFFFF";
|
|
1978
|
+
ctx.fillRect(0, 0, width, height);
|
|
1979
|
+
|
|
1980
|
+
// Draw the SVG image at logical size
|
|
1981
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
1982
|
+
|
|
1983
|
+
// Clean up URL
|
|
1984
|
+
URL.revokeObjectURL(url);
|
|
1985
|
+
|
|
1986
|
+
// Return data URL
|
|
1987
|
+
resolve(canvas.toDataURL('image/png'));
|
|
1988
|
+
};
|
|
1989
|
+
|
|
1990
|
+
img.onerror = () => {
|
|
1991
|
+
URL.revokeObjectURL(url);
|
|
1992
|
+
reject(new Error('Failed to load SVG image'));
|
|
1993
|
+
};
|
|
1994
|
+
|
|
1995
|
+
img.src = url;
|
|
1996
|
+
});
|
|
1997
|
+
|
|
1998
|
+
// Replace math element with img tag containing the PNG data URL
|
|
1999
|
+
const imgElement = document.createElement('img');
|
|
2000
|
+
imgElement.src = dataUrl;
|
|
2001
|
+
|
|
2002
|
+
// Extract dimensions from the data URL canvas
|
|
2003
|
+
const img2 = new Image();
|
|
2004
|
+
img2.src = dataUrl;
|
|
2005
|
+
await new Promise((resolve) => {
|
|
2006
|
+
img2.onload = resolve;
|
|
2007
|
+
img2.onerror = resolve;
|
|
2008
|
+
setTimeout(resolve, 100); // Timeout fallback
|
|
2009
|
+
});
|
|
2010
|
+
|
|
2011
|
+
// Set explicit dimensions (accounting for DPR)
|
|
2012
|
+
const displayWidth = img2.naturalWidth / 2; // Divide by DPR
|
|
2013
|
+
const displayHeight = img2.naturalHeight / 2;
|
|
2014
|
+
|
|
2015
|
+
imgElement.width = displayWidth;
|
|
2016
|
+
imgElement.height = displayHeight;
|
|
2017
|
+
imgElement.style.cssText = `display:inline-block;margin:0.5em 0;width:${displayWidth}px;height:${displayHeight}px;vertical-align:middle;`;
|
|
2018
|
+
imgElement.alt = 'Math equation';
|
|
2019
|
+
|
|
2020
|
+
mathEl.parentNode.replaceChild(imgElement, mathEl);
|
|
2021
|
+
} catch (error) {
|
|
2022
|
+
console.error('Failed to convert math element to image:', error);
|
|
2023
|
+
// Keep the original element if conversion fails
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
// 2. Process GeoJSON maps - convert to static images (following Gem's guide)
|
|
2029
|
+
const geojsonContainers = clone.querySelectorAll('.geojson-container');
|
|
2030
|
+
if (geojsonContainers.length > 0) {
|
|
2031
|
+
|
|
2032
|
+
for (const clonedContainer of geojsonContainers) {
|
|
2033
|
+
try {
|
|
2034
|
+
// Find the corresponding live container by matching data-original-source
|
|
2035
|
+
const originalSource = clonedContainer.getAttribute('data-original-source');
|
|
2036
|
+
if (!originalSource) {
|
|
2037
|
+
console.warn('No original source found for GeoJSON container');
|
|
2038
|
+
continue;
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
// Find live container with same source
|
|
2042
|
+
let liveContainer = null;
|
|
2043
|
+
const allLiveContainers = previewPanel.querySelectorAll('.geojson-container');
|
|
2044
|
+
for (const candidate of allLiveContainers) {
|
|
2045
|
+
if (candidate.getAttribute('data-original-source') === originalSource) {
|
|
2046
|
+
liveContainer = candidate;
|
|
2047
|
+
break;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
if (!liveContainer) {
|
|
2052
|
+
console.warn('Could not find live GeoJSON container');
|
|
2053
|
+
const placeholder = document.createElement('div');
|
|
2054
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
2055
|
+
placeholder.textContent = '[GeoJSON Map - Interactive content not available in copy]';
|
|
2056
|
+
clonedContainer.parentNode.replaceChild(placeholder, clonedContainer);
|
|
2057
|
+
continue;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
// Check if map is ready
|
|
2061
|
+
const map = liveContainer._map;
|
|
2062
|
+
if (!map) {
|
|
2063
|
+
console.warn('Map not initialized yet');
|
|
2064
|
+
const placeholder = document.createElement('div');
|
|
2065
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
2066
|
+
placeholder.textContent = '[GeoJSON Map - Still loading]';
|
|
2067
|
+
clonedContainer.parentNode.replaceChild(placeholder, clonedContainer);
|
|
2068
|
+
continue;
|
|
2069
|
+
}
|
|
2070
|
+
|
|
2071
|
+
// Rasterize the map to PNG
|
|
2072
|
+
const dataUrl = await rasterizeGeoJSONMap(liveContainer);
|
|
2073
|
+
|
|
2074
|
+
if (dataUrl) {
|
|
2075
|
+
// Replace with image
|
|
2076
|
+
const img = document.createElement('img');
|
|
2077
|
+
img.src = dataUrl;
|
|
2078
|
+
img.style.cssText = 'width: 100%; height: 300px; border: 1px solid #ddd; border-radius: 4px; margin: 0.5em 0;';
|
|
2079
|
+
img.alt = 'GeoJSON Map';
|
|
2080
|
+
clonedContainer.parentNode.replaceChild(img, clonedContainer);
|
|
2081
|
+
} else {
|
|
2082
|
+
// Fallback placeholder
|
|
2083
|
+
const placeholder = document.createElement('div');
|
|
2084
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
2085
|
+
placeholder.textContent = '[GeoJSON Map - Interactive content not available in copy]';
|
|
2086
|
+
clonedContainer.parentNode.replaceChild(placeholder, clonedContainer);
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
} catch (error) {
|
|
2090
|
+
console.error('Failed to process GeoJSON container:', error);
|
|
2091
|
+
// Replace with placeholder
|
|
2092
|
+
const placeholder = document.createElement('div');
|
|
2093
|
+
placeholder.style.cssText = 'padding: 12px; background-color: #f0f0f0; border: 1px solid #ccc; text-align: center; margin: 0.5em 0; border-radius: 4px;';
|
|
2094
|
+
placeholder.textContent = '[GeoJSON Map - Interactive content not available in copy]';
|
|
2095
|
+
clonedContainer.parentNode.replaceChild(placeholder, clonedContainer);
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
// 6. Process GeoJSON/Leaflet maps - capture as single image (compose tiles + overlays)
|
|
2103
|
+
const mapContainers = clone.querySelectorAll('[data-qd-lang="geojson"]');
|
|
2104
|
+
for (const container of mapContainers) {
|
|
2105
|
+
try {
|
|
2106
|
+
const containerId = container.id;
|
|
2107
|
+
const originalContainer = containerId ? previewPanel.querySelector(`#${containerId}`) : null;
|
|
2108
|
+
if (!originalContainer) continue;
|
|
2109
|
+
const leafletContainer = originalContainer.querySelector('.leaflet-container');
|
|
2110
|
+
if (!leafletContainer) continue;
|
|
2111
|
+
|
|
2112
|
+
const dpr = Math.max(1, window.devicePixelRatio || 1);
|
|
2113
|
+
const width = leafletContainer.clientWidth || 600;
|
|
2114
|
+
const height = leafletContainer.clientHeight || 400;
|
|
2115
|
+
const canvas = document.createElement('canvas');
|
|
2116
|
+
canvas.width = Math.round(width * dpr);
|
|
2117
|
+
canvas.height = Math.round(height * dpr);
|
|
2118
|
+
const ctx = canvas.getContext('2d');
|
|
2119
|
+
ctx.scale(dpr, dpr);
|
|
2120
|
+
ctx.fillStyle = '#FFFFFF';
|
|
2121
|
+
ctx.fillRect(0, 0, width, height);
|
|
2122
|
+
|
|
2123
|
+
const leafRect = leafletContainer.getBoundingClientRect();
|
|
2124
|
+
|
|
2125
|
+
// Draw tiles (snap to integer pixels to avoid seams)
|
|
2126
|
+
const tiles = Array.from(leafletContainer.querySelectorAll('img.leaflet-tile'));
|
|
2127
|
+
for (const tile of tiles) {
|
|
2128
|
+
try {
|
|
2129
|
+
const r = tile.getBoundingClientRect();
|
|
2130
|
+
const x = Math.round(r.left - leafRect.left);
|
|
2131
|
+
const y = Math.round(r.top - leafRect.top);
|
|
2132
|
+
const w = Math.round(r.width);
|
|
2133
|
+
const h = Math.round(r.height);
|
|
2134
|
+
const overlaps = !(r.right <= leafRect.left || r.left >= leafRect.right || r.bottom <= leafRect.top || r.top >= leafRect.bottom);
|
|
2135
|
+
const style = window.getComputedStyle(tile);
|
|
2136
|
+
if (w > 0 && h > 0 && overlaps && style.display !== 'none' && style.visibility !== 'hidden') {
|
|
2137
|
+
ctx.drawImage(tile, x, y, w + 1, h + 1);
|
|
2138
|
+
}
|
|
2139
|
+
} catch (e) {
|
|
2140
|
+
console.warn('Failed to draw tile:', e);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
// Draw SVG overlays (paths, markers)
|
|
2145
|
+
const overlaySvgs = originalContainer.querySelectorAll('.leaflet-overlay-pane svg');
|
|
2146
|
+
for (const svg of overlaySvgs) {
|
|
2147
|
+
try {
|
|
2148
|
+
const svgStr = new XMLSerializer().serializeToString(svg);
|
|
2149
|
+
const dataUrl = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgStr);
|
|
2150
|
+
const img = new Image();
|
|
2151
|
+
await new Promise((resolve) => { img.onload = resolve; img.onerror = resolve; img.src = dataUrl; });
|
|
2152
|
+
const r = svg.getBoundingClientRect();
|
|
2153
|
+
const x = Math.round(r.left - leafRect.left);
|
|
2154
|
+
const y = Math.round(r.top - leafRect.top);
|
|
2155
|
+
const w = Math.round(r.width);
|
|
2156
|
+
const h = Math.round(r.height);
|
|
2157
|
+
const overlaps = !(r.right <= leafRect.left || r.left >= leafRect.right || r.bottom <= leafRect.top || r.top >= leafRect.bottom);
|
|
2158
|
+
if (w > 0 && h > 0 && overlaps) ctx.drawImage(img, x, y, w, h);
|
|
2159
|
+
} catch (e) {
|
|
2160
|
+
console.warn('Failed to draw overlay SVG:', e);
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
// Draw marker icons (PNG/SVG img elements)
|
|
2165
|
+
const markerIcons = originalContainer.querySelectorAll('.leaflet-marker-pane img.leaflet-marker-icon');
|
|
2166
|
+
for (const icon of markerIcons) {
|
|
2167
|
+
try {
|
|
2168
|
+
const r = icon.getBoundingClientRect();
|
|
2169
|
+
const x = Math.round(r.left - leafRect.left);
|
|
2170
|
+
const y = Math.round(r.top - leafRect.top);
|
|
2171
|
+
const w = Math.round(r.width);
|
|
2172
|
+
const h = Math.round(r.height);
|
|
2173
|
+
const overlaps = !(r.right <= leafRect.left || r.left >= leafRect.right || r.bottom <= leafRect.top || r.top >= leafRect.bottom);
|
|
2174
|
+
const style = window.getComputedStyle(icon);
|
|
2175
|
+
if (w > 0 && h > 0 && overlaps && style.display !== 'none' && style.visibility !== 'hidden') {
|
|
2176
|
+
ctx.drawImage(icon, x, y, w, h);
|
|
2177
|
+
}
|
|
2178
|
+
} catch (e) {
|
|
2179
|
+
console.warn('Failed to draw marker icon:', e);
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
// Try to produce a data URL (may fail if canvas tainted by CORS tiles)
|
|
2184
|
+
let mapDataUrl = '';
|
|
2185
|
+
try {
|
|
2186
|
+
mapDataUrl = canvas.toDataURL('image/png', 1.0);
|
|
2187
|
+
} catch (e) {
|
|
2188
|
+
console.warn('Map canvas tainted; falling back to placeholder');
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
const img = document.createElement('img');
|
|
2192
|
+
if (mapDataUrl) {
|
|
2193
|
+
img.src = mapDataUrl;
|
|
2194
|
+
img.width = width;
|
|
2195
|
+
img.height = height;
|
|
2196
|
+
img.setAttribute('width', String(width));
|
|
2197
|
+
img.setAttribute('height', String(height));
|
|
2198
|
+
img.style.width = width + 'px';
|
|
2199
|
+
img.style.height = height + 'px';
|
|
2200
|
+
img.style.display = 'block';
|
|
2201
|
+
img.style.border = '1px solid #ddd';
|
|
2202
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
2203
|
+
img.alt = 'Map';
|
|
2204
|
+
} else {
|
|
2205
|
+
img.alt = 'Map';
|
|
2206
|
+
img.style.width = width + 'px';
|
|
2207
|
+
img.style.height = height + 'px';
|
|
2208
|
+
img.style.border = '1px solid #ddd';
|
|
2209
|
+
img.style.backgroundColor = '#f0f0f0';
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
container.parentNode.replaceChild(img, container);
|
|
2213
|
+
} catch (err) {
|
|
2214
|
+
console.warn('Failed to process map container:', err);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
// 7. Process HTML fence blocks - render the HTML content and process images
|
|
2219
|
+
const htmlContainers = clone.querySelectorAll('.qde-html-container');
|
|
2220
|
+
for (const container of htmlContainers) {
|
|
2221
|
+
try {
|
|
2222
|
+
// Get the original source HTML
|
|
2223
|
+
const source = container.getAttribute('data-qd-source');
|
|
2224
|
+
|
|
2225
|
+
// Check if there's a pre element (fallback display) or actual HTML content
|
|
2226
|
+
const pre = container.querySelector('pre');
|
|
2227
|
+
|
|
2228
|
+
if (source) {
|
|
2229
|
+
// Parse the source HTML
|
|
2230
|
+
const tempDiv = document.createElement('div');
|
|
2231
|
+
tempDiv.innerHTML = source;
|
|
2232
|
+
|
|
2233
|
+
// Process all images in the HTML block
|
|
2234
|
+
const htmlImages = tempDiv.querySelectorAll('img');
|
|
2235
|
+
for (const img of htmlImages) {
|
|
2236
|
+
// Preserve original dimensions from HTML attributes
|
|
2237
|
+
const widthAttr = img.getAttribute('width');
|
|
2238
|
+
const heightAttr = img.getAttribute('height');
|
|
2239
|
+
|
|
2240
|
+
if (widthAttr) {
|
|
2241
|
+
img.width = parseInt(widthAttr);
|
|
2242
|
+
img.style.width = widthAttr.includes('%') ? widthAttr : `${img.width}px`;
|
|
2243
|
+
}
|
|
2244
|
+
if (heightAttr) {
|
|
2245
|
+
img.height = parseInt(heightAttr);
|
|
2246
|
+
img.style.height = heightAttr.includes('%') ? heightAttr : `${img.height}px`;
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
// Convert to data URL using canvas (like squibview)
|
|
2250
|
+
if (img.src && !img.src.startsWith('data:')) {
|
|
2251
|
+
try {
|
|
2252
|
+
// Use canvas to convert image to data URL (avoids CORS issues)
|
|
2253
|
+
const canvas = document.createElement('canvas');
|
|
2254
|
+
const ctx = canvas.getContext('2d');
|
|
2255
|
+
|
|
2256
|
+
// Create new image and wait for it to load
|
|
2257
|
+
const tempImg = new Image();
|
|
2258
|
+
tempImg.crossOrigin = 'anonymous';
|
|
2259
|
+
|
|
2260
|
+
await new Promise((resolve, reject) => {
|
|
2261
|
+
tempImg.onload = function() {
|
|
2262
|
+
|
|
2263
|
+
// Calculate dimensions preserving aspect ratio
|
|
2264
|
+
let displayWidth = 0;
|
|
2265
|
+
let displayHeight = 0;
|
|
2266
|
+
|
|
2267
|
+
// Use the width specified in HTML (e.g. width="250")
|
|
2268
|
+
if (widthAttr && !widthAttr.includes('%')) {
|
|
2269
|
+
displayWidth = parseInt(widthAttr);
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
// Use the height if specified
|
|
2273
|
+
if (heightAttr && !heightAttr.includes('%')) {
|
|
2274
|
+
displayHeight = parseInt(heightAttr);
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
|
|
2278
|
+
// If only width is specified, calculate height based on aspect ratio
|
|
2279
|
+
if (displayWidth > 0 && displayHeight === 0) {
|
|
2280
|
+
if (tempImg.naturalWidth > 0) {
|
|
2281
|
+
const aspectRatio = tempImg.naturalHeight / tempImg.naturalWidth;
|
|
2282
|
+
displayHeight = Math.round(displayWidth * aspectRatio);
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
// If only height is specified, calculate width based on aspect ratio
|
|
2286
|
+
else if (displayHeight > 0 && displayWidth === 0) {
|
|
2287
|
+
if (tempImg.naturalHeight > 0) {
|
|
2288
|
+
const aspectRatio = tempImg.naturalWidth / tempImg.naturalHeight;
|
|
2289
|
+
displayWidth = Math.round(displayHeight * aspectRatio);
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
// If neither specified, use natural dimensions
|
|
2293
|
+
else if (displayWidth === 0 && displayHeight === 0) {
|
|
2294
|
+
displayWidth = tempImg.naturalWidth || 250;
|
|
2295
|
+
displayHeight = tempImg.naturalHeight || 200;
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
canvas.width = displayWidth;
|
|
2300
|
+
canvas.height = displayHeight;
|
|
2301
|
+
|
|
2302
|
+
// Draw image to canvas
|
|
2303
|
+
ctx.drawImage(tempImg, 0, 0, displayWidth, displayHeight);
|
|
2304
|
+
|
|
2305
|
+
// Convert to data URL
|
|
2306
|
+
const dataUrl = canvas.toDataURL('image/png', 1.0);
|
|
2307
|
+
|
|
2308
|
+
// Update original image
|
|
2309
|
+
img.src = dataUrl;
|
|
2310
|
+
img.width = displayWidth;
|
|
2311
|
+
img.height = displayHeight;
|
|
2312
|
+
img.setAttribute('width', displayWidth.toString());
|
|
2313
|
+
img.setAttribute('height', displayHeight.toString());
|
|
2314
|
+
img.style.width = displayWidth + 'px';
|
|
2315
|
+
img.style.height = displayHeight + 'px';
|
|
2316
|
+
|
|
2317
|
+
resolve();
|
|
2318
|
+
};
|
|
2319
|
+
|
|
2320
|
+
tempImg.onerror = function() {
|
|
2321
|
+
console.warn('Failed to load HTML fence image:', img.src);
|
|
2322
|
+
reject(new Error('Image load failed'));
|
|
2323
|
+
};
|
|
2324
|
+
|
|
2325
|
+
// Set source - resolve relative paths
|
|
2326
|
+
if (img.src.startsWith('http') || img.src.startsWith('//')) {
|
|
2327
|
+
tempImg.src = img.src;
|
|
2328
|
+
} else {
|
|
2329
|
+
// Relative path - let browser resolve it
|
|
2330
|
+
const absoluteImg = new Image();
|
|
2331
|
+
absoluteImg.src = img.src;
|
|
2332
|
+
tempImg.src = absoluteImg.src;
|
|
2333
|
+
}
|
|
2334
|
+
});
|
|
2335
|
+
} catch (err) {
|
|
2336
|
+
console.warn('Failed to convert HTML fence image:', img.src, err);
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
|
|
2340
|
+
// Add v:shapes for Word compatibility
|
|
2341
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
// Replace container content with processed HTML (whether it had pre or not)
|
|
2345
|
+
container.innerHTML = tempDiv.innerHTML;
|
|
2346
|
+
} else if (!pre) {
|
|
2347
|
+
// Container has rendered HTML already, process its images directly
|
|
2348
|
+
const htmlImages = container.querySelectorAll('img');
|
|
2349
|
+
for (const img of htmlImages) {
|
|
2350
|
+
// Same image processing as above
|
|
2351
|
+
const widthAttr = img.getAttribute('width');
|
|
2352
|
+
const heightAttr = img.getAttribute('height');
|
|
2353
|
+
|
|
2354
|
+
if (widthAttr) {
|
|
2355
|
+
img.width = parseInt(widthAttr);
|
|
2356
|
+
img.style.width = widthAttr.includes('%') ? widthAttr : `${img.width}px`;
|
|
2357
|
+
}
|
|
2358
|
+
if (heightAttr) {
|
|
2359
|
+
img.height = parseInt(heightAttr);
|
|
2360
|
+
img.style.height = heightAttr.includes('%') ? heightAttr : `${img.height}px`;
|
|
2361
|
+
}
|
|
2362
|
+
|
|
2363
|
+
if (img.src && !img.src.startsWith('data:')) {
|
|
2364
|
+
try {
|
|
2365
|
+
// Use same canvas approach as above
|
|
2366
|
+
const canvas = document.createElement('canvas');
|
|
2367
|
+
const ctx = canvas.getContext('2d');
|
|
2368
|
+
const tempImg = new Image();
|
|
2369
|
+
tempImg.crossOrigin = 'anonymous';
|
|
2370
|
+
|
|
2371
|
+
await new Promise((resolve, reject) => {
|
|
2372
|
+
tempImg.onload = function() {
|
|
2373
|
+
// Calculate dimensions preserving aspect ratio
|
|
2374
|
+
let displayWidth = img.width || 0;
|
|
2375
|
+
let displayHeight = img.height || 0;
|
|
2376
|
+
|
|
2377
|
+
// If only width is specified, calculate height based on aspect ratio
|
|
2378
|
+
if (displayWidth && !displayHeight) {
|
|
2379
|
+
const aspectRatio = tempImg.naturalHeight / tempImg.naturalWidth;
|
|
2380
|
+
displayHeight = Math.round(displayWidth * aspectRatio);
|
|
2381
|
+
}
|
|
2382
|
+
// If only height is specified, calculate width based on aspect ratio
|
|
2383
|
+
else if (displayHeight && !displayWidth) {
|
|
2384
|
+
const aspectRatio = tempImg.naturalWidth / tempImg.naturalHeight;
|
|
2385
|
+
displayWidth = Math.round(displayHeight * aspectRatio);
|
|
2386
|
+
}
|
|
2387
|
+
// If neither specified, use natural dimensions
|
|
2388
|
+
else if (!displayWidth && !displayHeight) {
|
|
2389
|
+
displayWidth = tempImg.naturalWidth || 250;
|
|
2390
|
+
displayHeight = tempImg.naturalHeight || Math.round(250 * (tempImg.naturalHeight / tempImg.naturalWidth));
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
canvas.width = displayWidth;
|
|
2394
|
+
canvas.height = displayHeight;
|
|
2395
|
+
ctx.drawImage(tempImg, 0, 0, displayWidth, displayHeight);
|
|
2396
|
+
|
|
2397
|
+
const dataUrl = canvas.toDataURL('image/png', 1.0);
|
|
2398
|
+
img.src = dataUrl;
|
|
2399
|
+
img.width = displayWidth;
|
|
2400
|
+
img.height = displayHeight;
|
|
2401
|
+
img.setAttribute('width', displayWidth.toString());
|
|
2402
|
+
img.setAttribute('height', displayHeight.toString());
|
|
2403
|
+
img.style.width = displayWidth + 'px';
|
|
2404
|
+
img.style.height = displayHeight + 'px';
|
|
2405
|
+
|
|
2406
|
+
resolve();
|
|
2407
|
+
};
|
|
2408
|
+
|
|
2409
|
+
tempImg.onerror = function() {
|
|
2410
|
+
console.warn('Failed to load HTML fence image:', img.src);
|
|
2411
|
+
reject(new Error('Image load failed'));
|
|
2412
|
+
};
|
|
2413
|
+
|
|
2414
|
+
if (img.src.startsWith('http') || img.src.startsWith('//')) {
|
|
2415
|
+
tempImg.src = img.src;
|
|
2416
|
+
} else {
|
|
2417
|
+
const absoluteImg = new Image();
|
|
2418
|
+
absoluteImg.src = img.src;
|
|
2419
|
+
tempImg.src = absoluteImg.src;
|
|
2420
|
+
}
|
|
2421
|
+
});
|
|
2422
|
+
} catch (err) {
|
|
2423
|
+
console.warn('Failed to convert HTML fence image:', img.src, err);
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
img.setAttribute('v:shapes', 'image' + Math.random().toString(36).substr(2, 9));
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
} catch (err) {
|
|
2431
|
+
console.warn('Failed to process HTML container:', err);
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
// 8. Tables are already HTML tables from the built-in renderer
|
|
2436
|
+
// No processing needed
|
|
2437
|
+
|
|
2438
|
+
// Wrap in proper HTML structure for rich text editors
|
|
2439
|
+
const fragment = clone.innerHTML;
|
|
2440
|
+
const htmlContent = `
|
|
2441
|
+
<!DOCTYPE html>
|
|
2442
|
+
<html xmlns:v="urn:schemas-microsoft-com:vml"
|
|
2443
|
+
xmlns:o="urn:schemas-microsoft-com:office:office"
|
|
2444
|
+
xmlns:w="urn:schemas-microsoft-com:office:word">
|
|
2445
|
+
<head>
|
|
2446
|
+
<meta charset="utf-8">
|
|
2447
|
+
<style>
|
|
2448
|
+
/* Table styling */
|
|
2449
|
+
table { border-collapse: collapse; width: 100%; margin-bottom: 1em; }
|
|
2450
|
+
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
|
|
2451
|
+
th { background-color: #f0f0f0; font-weight: bold; }
|
|
2452
|
+
|
|
2453
|
+
/* Code block styling */
|
|
2454
|
+
pre { background-color: #f4f4f4; padding: 1em; border-radius: 4px; overflow-x: auto; }
|
|
2455
|
+
code { font-family: monospace; background-color: #f4f4f4; padding: 0.2em 0.4em; border-radius: 3px; }
|
|
2456
|
+
|
|
2457
|
+
/* Image handling */
|
|
2458
|
+
img { display: block; max-width: 100%; height: auto; margin: 0.5em 0; }
|
|
2459
|
+
|
|
2460
|
+
/* Blockquote */
|
|
2461
|
+
blockquote { border-left: 4px solid #ddd; margin-left: 0; padding-left: 1em; color: #666; }
|
|
2462
|
+
|
|
2463
|
+
/* Math equations centered like squibview */
|
|
2464
|
+
.math-display { text-align: center; margin: 1em 0; }
|
|
2465
|
+
.math-display img { display: inline-block; margin: 0 auto; }
|
|
2466
|
+
</style>
|
|
2467
|
+
</head>
|
|
2468
|
+
<body><!--StartFragment-->${fragment}<!--EndFragment--></body>
|
|
2469
|
+
</html>`;
|
|
2470
|
+
|
|
2471
|
+
// Get plain text version
|
|
2472
|
+
const text = clone.textContent || clone.innerText || '';
|
|
2473
|
+
|
|
2474
|
+
// Get platform for clipboard strategy (like squibview)
|
|
2475
|
+
const platform = getPlatform();
|
|
2476
|
+
|
|
2477
|
+
if (platform === 'macos') {
|
|
2478
|
+
// macOS approach (like squibview)
|
|
2479
|
+
try {
|
|
2480
|
+
await navigator.clipboard.write([
|
|
2481
|
+
new ClipboardItem({
|
|
2482
|
+
'text/html': new Blob([htmlContent], { type: 'text/html' }),
|
|
2483
|
+
'text/plain': new Blob([text], { type: 'text/plain' })
|
|
2484
|
+
})
|
|
2485
|
+
]);
|
|
2486
|
+
return { success: true, html: htmlContent, text };
|
|
2487
|
+
} catch (modernErr) {
|
|
2488
|
+
console.warn('Modern clipboard API failed, trying Safari fallback:', modernErr);
|
|
2489
|
+
// Safari fallback (selection-based HTML of fragment)
|
|
2490
|
+
if (copyToClipboard(fragment)) {
|
|
2491
|
+
return { success: true, html: htmlContent, text };
|
|
2492
|
+
}
|
|
2493
|
+
throw new Error('Fallback copy failed');
|
|
2494
|
+
}
|
|
2495
|
+
} else {
|
|
2496
|
+
// Windows/Linux approach (like squibview)
|
|
2497
|
+
const tempDiv = document.createElement('div');
|
|
2498
|
+
tempDiv.style.position = 'fixed';
|
|
2499
|
+
tempDiv.style.left = '-9999px';
|
|
2500
|
+
tempDiv.style.top = '0';
|
|
2501
|
+
// Use fragment for selection-based fallback copy
|
|
2502
|
+
tempDiv.innerHTML = fragment;
|
|
2503
|
+
document.body.appendChild(tempDiv);
|
|
2504
|
+
|
|
2505
|
+
try {
|
|
2506
|
+
await navigator.clipboard.write([
|
|
2507
|
+
new ClipboardItem({
|
|
2508
|
+
'text/html': new Blob([htmlContent], { type: 'text/html' }),
|
|
2509
|
+
'text/plain': new Blob([text], { type: 'text/plain' })
|
|
2510
|
+
})
|
|
2511
|
+
]);
|
|
2512
|
+
return { success: true, html: htmlContent, text };
|
|
2513
|
+
} catch (modernErr) {
|
|
2514
|
+
console.warn('Modern clipboard API failed, trying execCommand fallback:', modernErr);
|
|
2515
|
+
const selection = window.getSelection();
|
|
2516
|
+
const range = document.createRange();
|
|
2517
|
+
range.selectNodeContents(tempDiv);
|
|
2518
|
+
selection.removeAllRanges();
|
|
2519
|
+
selection.addRange(range);
|
|
2520
|
+
|
|
2521
|
+
const successful = document.execCommand('copy');
|
|
2522
|
+
if (!successful) {
|
|
2523
|
+
throw new Error('Fallback copy failed');
|
|
2524
|
+
}
|
|
2525
|
+
return { success: true, html: htmlContent, text };
|
|
2526
|
+
} finally {
|
|
2527
|
+
if (tempDiv && tempDiv.parentNode) {
|
|
2528
|
+
document.body.removeChild(tempDiv);
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
|
|
2533
|
+
} catch (err) {
|
|
2534
|
+
console.error('Failed to copy rendered content:', err);
|
|
2535
|
+
throw err;
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
|
|
1073
2539
|
/**
|
|
1074
2540
|
* Quikdown Editor - A drop-in markdown editor control
|
|
1075
2541
|
* @version 1.0.5
|
|
@@ -1217,7 +2683,8 @@
|
|
|
1217
2683
|
// Copy buttons
|
|
1218
2684
|
const copyButtons = [
|
|
1219
2685
|
{ action: 'copy-markdown', text: 'Copy MD', title: 'Copy markdown to clipboard' },
|
|
1220
|
-
{ action: 'copy-html', text: 'Copy HTML', title: 'Copy HTML to clipboard' }
|
|
2686
|
+
{ action: 'copy-html', text: 'Copy HTML', title: 'Copy HTML to clipboard' },
|
|
2687
|
+
{ action: 'copy-rendered', text: 'Copy Rendered', title: 'Copy rich text to clipboard' }
|
|
1221
2688
|
];
|
|
1222
2689
|
|
|
1223
2690
|
copyButtons.forEach(({ action, text, title }) => {
|
|
@@ -1623,6 +3090,24 @@
|
|
|
1623
3090
|
this.previewPanel.innerHTML = this._html;
|
|
1624
3091
|
// Make all fence blocks non-editable
|
|
1625
3092
|
this.makeFencesNonEditable();
|
|
3093
|
+
|
|
3094
|
+
// Process all math elements with MathJax if loaded (like squibview)
|
|
3095
|
+
if (window.MathJax && window.MathJax.typesetPromise) {
|
|
3096
|
+
const mathElements = this.previewPanel.querySelectorAll('.math-display');
|
|
3097
|
+
if (mathElements.length > 0) {
|
|
3098
|
+
mathElements.forEach(el => {
|
|
3099
|
+
});
|
|
3100
|
+
window.MathJax.typesetPromise(Array.from(mathElements))
|
|
3101
|
+
.then(() => {
|
|
3102
|
+
mathElements.forEach(el => {
|
|
3103
|
+
el.querySelector('mjx-container');
|
|
3104
|
+
});
|
|
3105
|
+
})
|
|
3106
|
+
.catch(err => {
|
|
3107
|
+
console.warn('MathJax batch processing failed:', err);
|
|
3108
|
+
});
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
1626
3111
|
}
|
|
1627
3112
|
}
|
|
1628
3113
|
|
|
@@ -1644,7 +3129,7 @@
|
|
|
1644
3129
|
|
|
1645
3130
|
this._html = this.previewPanel.innerHTML;
|
|
1646
3131
|
this._markdown = quikdown_bd.toMarkdown(clonedPanel, {
|
|
1647
|
-
fence_plugin: this.
|
|
3132
|
+
fence_plugin: this.createFencePlugin()
|
|
1648
3133
|
});
|
|
1649
3134
|
|
|
1650
3135
|
// Update source if visible
|
|
@@ -1664,7 +3149,6 @@
|
|
|
1664
3149
|
preprocessSpecialElements(panel) {
|
|
1665
3150
|
if (!panel) return;
|
|
1666
3151
|
|
|
1667
|
-
|
|
1668
3152
|
// Restore non-editable complex fences from their data attributes
|
|
1669
3153
|
const complexFences = panel.querySelectorAll('[contenteditable="false"][data-qd-source]');
|
|
1670
3154
|
complexFences.forEach(element => {
|
|
@@ -1762,7 +3246,6 @@
|
|
|
1762
3246
|
return this.renderHTML(code);
|
|
1763
3247
|
|
|
1764
3248
|
case 'math':
|
|
1765
|
-
case 'katex':
|
|
1766
3249
|
case 'tex':
|
|
1767
3250
|
case 'latex':
|
|
1768
3251
|
return this.renderMath(code, lang);
|
|
@@ -1776,11 +3259,20 @@
|
|
|
1776
3259
|
case 'json5':
|
|
1777
3260
|
return this.renderJSON(code, lang);
|
|
1778
3261
|
|
|
3262
|
+
case 'katex': // Use MathJax for katex fence blocks (backward compatibility)
|
|
3263
|
+
return this.renderMath(code, 'katex');
|
|
3264
|
+
|
|
1779
3265
|
case 'mermaid':
|
|
1780
3266
|
if (window.mermaid) {
|
|
1781
3267
|
return this.renderMermaid(code);
|
|
1782
3268
|
}
|
|
1783
3269
|
break;
|
|
3270
|
+
|
|
3271
|
+
case 'geojson':
|
|
3272
|
+
return this.renderGeoJSON(code);
|
|
3273
|
+
|
|
3274
|
+
case 'stl':
|
|
3275
|
+
return this.renderSTL(code);
|
|
1784
3276
|
}
|
|
1785
3277
|
}
|
|
1786
3278
|
|
|
@@ -1795,8 +3287,37 @@
|
|
|
1795
3287
|
return undefined;
|
|
1796
3288
|
};
|
|
1797
3289
|
|
|
1798
|
-
//
|
|
1799
|
-
|
|
3290
|
+
// Reverse function to extract raw source from rendered HTML
|
|
3291
|
+
const reverse = (element) => {
|
|
3292
|
+
// Get the language from data attribute
|
|
3293
|
+
const lang = element.getAttribute('data-qd-lang') || '';
|
|
3294
|
+
let content = '';
|
|
3295
|
+
|
|
3296
|
+
// For syntax-highlighted code, extract the raw text
|
|
3297
|
+
if (element.querySelector('code.hljs')) {
|
|
3298
|
+
const code = element.querySelector('code.hljs');
|
|
3299
|
+
content = code.textContent || code.innerText || '';
|
|
3300
|
+
}
|
|
3301
|
+
// For other code blocks, just get the text content
|
|
3302
|
+
else if (element.querySelector('code')) {
|
|
3303
|
+
const codeEl = element.querySelector('code');
|
|
3304
|
+
content = codeEl.textContent || codeEl.innerText || '';
|
|
3305
|
+
}
|
|
3306
|
+
// Fallback to element text
|
|
3307
|
+
else {
|
|
3308
|
+
content = element.textContent || element.innerText || '';
|
|
3309
|
+
}
|
|
3310
|
+
|
|
3311
|
+
// Return in the format quikdown_bd expects
|
|
3312
|
+
return {
|
|
3313
|
+
content: content,
|
|
3314
|
+
lang: lang,
|
|
3315
|
+
fence: '```'
|
|
3316
|
+
};
|
|
3317
|
+
};
|
|
3318
|
+
|
|
3319
|
+
// Return object format for v1.1.0 API with both render and reverse
|
|
3320
|
+
return { render, reverse };
|
|
1800
3321
|
}
|
|
1801
3322
|
|
|
1802
3323
|
/**
|
|
@@ -1908,80 +3429,88 @@
|
|
|
1908
3429
|
}
|
|
1909
3430
|
|
|
1910
3431
|
/**
|
|
1911
|
-
* Render math with
|
|
3432
|
+
* Render math with MathJax (SVG output for better copy support)
|
|
1912
3433
|
*/
|
|
1913
3434
|
renderMath(code, lang) {
|
|
1914
|
-
const id = `math-${
|
|
3435
|
+
const id = `math-${Math.random().toString(36).substring(2, 15)}`;
|
|
1915
3436
|
|
|
1916
|
-
//
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
const errorContainer = document.createElement('pre');
|
|
1936
|
-
errorContainer.className = 'qde-error';
|
|
1937
|
-
errorContainer.contentEditable = 'false';
|
|
1938
|
-
errorContainer.setAttribute('data-qd-fence', '```');
|
|
1939
|
-
errorContainer.setAttribute('data-qd-lang', lang);
|
|
1940
|
-
errorContainer.setAttribute('data-qd-source', code);
|
|
1941
|
-
errorContainer.textContent = `Math error: ${err.message}`;
|
|
1942
|
-
return errorContainer.outerHTML;
|
|
1943
|
-
}
|
|
3437
|
+
// Create container exactly like squibview
|
|
3438
|
+
const container = document.createElement('div');
|
|
3439
|
+
container.id = id;
|
|
3440
|
+
container.className = 'math-display';
|
|
3441
|
+
container.contentEditable = 'false';
|
|
3442
|
+
container.setAttribute('data-source-type', 'math');
|
|
3443
|
+
|
|
3444
|
+
// Format content for MathJax (display mode with $$) - exactly like squibview
|
|
3445
|
+
const singleLineContent = code.replace(/\r?\n/g, ' ').replace(/\s+/g, ' ').trim();
|
|
3446
|
+
container.textContent = `$$${singleLineContent}$$`;
|
|
3447
|
+
|
|
3448
|
+
// Add centering style
|
|
3449
|
+
container.style.textAlign = 'center';
|
|
3450
|
+
container.style.margin = '1em 0';
|
|
3451
|
+
|
|
3452
|
+
|
|
3453
|
+
// Ensure MathJax will be loaded (if not already)
|
|
3454
|
+
if (!window.MathJax || !window.MathJax.typesetPromise) {
|
|
3455
|
+
this.ensureMathJaxLoaded();
|
|
1944
3456
|
}
|
|
1945
3457
|
|
|
1946
|
-
//
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
3458
|
+
// MathJax will be processed in batch after preview update
|
|
3459
|
+
return container.outerHTML;
|
|
3460
|
+
}
|
|
3461
|
+
|
|
3462
|
+
/**
|
|
3463
|
+
* Ensures MathJax is loaded (but doesn't process elements)
|
|
3464
|
+
*/
|
|
3465
|
+
ensureMathJaxLoaded() {
|
|
3466
|
+
if (typeof window.MathJax === 'undefined' && !window.mathJaxLoading) {
|
|
3467
|
+
window.mathJaxLoading = true;
|
|
3468
|
+
|
|
3469
|
+
// Configure MathJax before loading
|
|
3470
|
+
if (!window.MathJax) {
|
|
3471
|
+
window.MathJax = {
|
|
3472
|
+
loader: { load: ['input/tex', 'output/svg'] },
|
|
3473
|
+
tex: {
|
|
3474
|
+
packages: { '[+]': ['ams'] },
|
|
3475
|
+
inlineMath: [['$', '$'], ['\\(', '\\)']],
|
|
3476
|
+
displayMath: [['$$', '$$'], ['\\[', '\\]']],
|
|
3477
|
+
processEscapes: true,
|
|
3478
|
+
processEnvironments: true
|
|
3479
|
+
},
|
|
3480
|
+
options: {
|
|
3481
|
+
renderActions: { addMenu: [] },
|
|
3482
|
+
ignoreHtmlClass: 'tex2jax_ignore',
|
|
3483
|
+
processHtmlClass: 'tex2jax_process'
|
|
3484
|
+
},
|
|
3485
|
+
svg: {
|
|
3486
|
+
fontCache: 'none' // Important: self-contained SVGs for copy
|
|
3487
|
+
},
|
|
3488
|
+
startup: { typeset: false }
|
|
3489
|
+
};
|
|
3490
|
+
}
|
|
3491
|
+
|
|
3492
|
+
const script = document.createElement('script');
|
|
3493
|
+
script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js';
|
|
3494
|
+
script.async = true;
|
|
3495
|
+
script.onload = () => {
|
|
3496
|
+
window.mathJaxLoading = false;
|
|
3497
|
+
|
|
3498
|
+
// Process any existing math elements (like squibview)
|
|
3499
|
+
if (window.MathJax && window.MathJax.typesetPromise) {
|
|
3500
|
+
const mathElements = document.querySelectorAll('.math-display');
|
|
3501
|
+
if (mathElements.length > 0) {
|
|
3502
|
+
window.MathJax.typesetPromise(Array.from(mathElements)).catch(err => {
|
|
3503
|
+
console.warn('Initial MathJax processing failed:', err);
|
|
1960
3504
|
});
|
|
1961
|
-
// Update attributes after rendering
|
|
1962
|
-
element.setAttribute('data-qd-source', code);
|
|
1963
|
-
element.setAttribute('data-qd-fence', '```');
|
|
1964
|
-
element.setAttribute('data-qd-lang', lang);
|
|
1965
|
-
} catch (err) {
|
|
1966
|
-
element.innerHTML = `<pre class="qde-error">Math error: ${this.escapeHtml(err.message)}</pre>`;
|
|
1967
3505
|
}
|
|
1968
3506
|
}
|
|
1969
|
-
}
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
placeholder.contentEditable = 'false';
|
|
1977
|
-
placeholder.setAttribute('data-qd-fence', '```');
|
|
1978
|
-
placeholder.setAttribute('data-qd-lang', lang);
|
|
1979
|
-
placeholder.setAttribute('data-qd-source', code);
|
|
1980
|
-
const pre = document.createElement('pre');
|
|
1981
|
-
pre.textContent = code;
|
|
1982
|
-
placeholder.appendChild(pre);
|
|
1983
|
-
|
|
1984
|
-
return placeholder.outerHTML;
|
|
3507
|
+
};
|
|
3508
|
+
script.onerror = () => {
|
|
3509
|
+
window.mathJaxLoading = false;
|
|
3510
|
+
console.error('Failed to load MathJax');
|
|
3511
|
+
};
|
|
3512
|
+
document.head.appendChild(script);
|
|
3513
|
+
}
|
|
1985
3514
|
}
|
|
1986
3515
|
|
|
1987
3516
|
/**
|
|
@@ -2088,6 +3617,237 @@
|
|
|
2088
3617
|
return `<pre class="qde-json" data-qd-fence="\`\`\`" data-qd-lang="${lang}">${this.escapeHtml(code)}</pre>`;
|
|
2089
3618
|
}
|
|
2090
3619
|
|
|
3620
|
+
/**
|
|
3621
|
+
* Render GeoJSON map
|
|
3622
|
+
*/
|
|
3623
|
+
renderGeoJSON(code) {
|
|
3624
|
+
// Generate unique map ID (following SquibView pattern)
|
|
3625
|
+
const mapId = `map-${Math.random().toString(36).substr(2, 15)}`;
|
|
3626
|
+
|
|
3627
|
+
// Function to render the map
|
|
3628
|
+
const renderMap = () => {
|
|
3629
|
+
const container = document.getElementById(mapId + '-container');
|
|
3630
|
+
if (!container || !window.L) return;
|
|
3631
|
+
|
|
3632
|
+
try {
|
|
3633
|
+
const data = JSON.parse(code);
|
|
3634
|
+
|
|
3635
|
+
// Clear container and set deterministic size for rasterization
|
|
3636
|
+
const mapDiv = document.createElement('div');
|
|
3637
|
+
mapDiv.id = mapId;
|
|
3638
|
+
mapDiv.style.cssText = 'width: 100%; height: 300px;';
|
|
3639
|
+
container.innerHTML = '';
|
|
3640
|
+
container.appendChild(mapDiv);
|
|
3641
|
+
|
|
3642
|
+
// Create the map
|
|
3643
|
+
const map = L.map(mapId);
|
|
3644
|
+
|
|
3645
|
+
// Store back-reference for capture (per Gem's guide)
|
|
3646
|
+
container._map = map; // Avoid window pollution
|
|
3647
|
+
|
|
3648
|
+
// Add tile layer with CORS support
|
|
3649
|
+
const tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
3650
|
+
attribution: '',
|
|
3651
|
+
crossOrigin: 'anonymous' // Important for canvas capture
|
|
3652
|
+
});
|
|
3653
|
+
tileLayer.addTo(map);
|
|
3654
|
+
|
|
3655
|
+
// Add GeoJSON layer
|
|
3656
|
+
const geoJsonLayer = L.geoJSON(data);
|
|
3657
|
+
geoJsonLayer.addTo(map);
|
|
3658
|
+
|
|
3659
|
+
// Fit bounds if valid
|
|
3660
|
+
if (geoJsonLayer.getBounds().isValid()) {
|
|
3661
|
+
map.fitBounds(geoJsonLayer.getBounds());
|
|
3662
|
+
} else {
|
|
3663
|
+
map.setView([0, 0], 2);
|
|
3664
|
+
}
|
|
3665
|
+
|
|
3666
|
+
// Store references for copy-time capture
|
|
3667
|
+
container._tileLayer = tileLayer;
|
|
3668
|
+
container._geoJsonLayer = geoJsonLayer;
|
|
3669
|
+
|
|
3670
|
+
// Optional: Wait for tiles to load for better capture
|
|
3671
|
+
tileLayer.on('load', () => {
|
|
3672
|
+
container.setAttribute('data-tiles-loaded', 'true');
|
|
3673
|
+
});
|
|
3674
|
+
|
|
3675
|
+
} catch (err) {
|
|
3676
|
+
container.innerHTML = `<pre class="qde-error">GeoJSON error: ${this.escapeHtml(err.message)}</pre>`;
|
|
3677
|
+
}
|
|
3678
|
+
};
|
|
3679
|
+
|
|
3680
|
+
// Check if Leaflet is already loaded
|
|
3681
|
+
if (window.L) {
|
|
3682
|
+
// Render after DOM update
|
|
3683
|
+
setTimeout(renderMap, 0);
|
|
3684
|
+
} else {
|
|
3685
|
+
// Lazy load Leaflet only if not already loading
|
|
3686
|
+
if (!window._qde_leaflet_loading) {
|
|
3687
|
+
window._qde_leaflet_loading = this.lazyLoadLibrary(
|
|
3688
|
+
'Leaflet',
|
|
3689
|
+
() => window.L,
|
|
3690
|
+
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js',
|
|
3691
|
+
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css'
|
|
3692
|
+
).catch(err => {
|
|
3693
|
+
console.warn('Failed to load Leaflet:', err);
|
|
3694
|
+
// Clear the loading promise so it can be retried
|
|
3695
|
+
window._qde_leaflet_loading = null;
|
|
3696
|
+
return false;
|
|
3697
|
+
});
|
|
3698
|
+
}
|
|
3699
|
+
|
|
3700
|
+
window._qde_leaflet_loading.then(loaded => {
|
|
3701
|
+
if (loaded) {
|
|
3702
|
+
renderMap();
|
|
3703
|
+
} else {
|
|
3704
|
+
const element = document.getElementById(id);
|
|
3705
|
+
if (element) {
|
|
3706
|
+
element.innerHTML = '<div style="padding: 20px; text-align: center; color: #666;">Failed to load map library</div>';
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
}).catch(() => {
|
|
3710
|
+
// Error already handled above
|
|
3711
|
+
});
|
|
3712
|
+
}
|
|
3713
|
+
|
|
3714
|
+
// Return container following SquibView pattern
|
|
3715
|
+
const container = document.createElement('div');
|
|
3716
|
+
container.className = 'geojson-container';
|
|
3717
|
+
container.id = mapId + '-container';
|
|
3718
|
+
container.style.cssText = 'width: 100%; height: 300px; border: 1px solid #ddd; border-radius: 4px; margin: 0.5em 0; background: #f0f0f0;';
|
|
3719
|
+
container.contentEditable = 'false';
|
|
3720
|
+
|
|
3721
|
+
// Preserve source for copy-time identification (per Gem's guide)
|
|
3722
|
+
container.setAttribute('data-source-type', 'geojson');
|
|
3723
|
+
container.setAttribute('data-original-source', this.escapeHtml(code));
|
|
3724
|
+
|
|
3725
|
+
// For bidirectional editing
|
|
3726
|
+
container.setAttribute('data-qd-fence', '```');
|
|
3727
|
+
container.setAttribute('data-qd-lang', 'geojson');
|
|
3728
|
+
container.setAttribute('data-qd-source', code);
|
|
3729
|
+
|
|
3730
|
+
container.textContent = 'Loading map...';
|
|
3731
|
+
|
|
3732
|
+
return container.outerHTML;
|
|
3733
|
+
}
|
|
3734
|
+
|
|
3735
|
+
/**
|
|
3736
|
+
* Render STL 3D model
|
|
3737
|
+
*/
|
|
3738
|
+
renderSTL(code) {
|
|
3739
|
+
const id = `qde-stl-viewer-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
3740
|
+
|
|
3741
|
+
// Function to render the 3D model
|
|
3742
|
+
const render3D = () => {
|
|
3743
|
+
const element = document.getElementById(id);
|
|
3744
|
+
if (!element) return;
|
|
3745
|
+
|
|
3746
|
+
// Check if Three.js is available
|
|
3747
|
+
if (typeof window.THREE === 'undefined') {
|
|
3748
|
+
element.innerHTML = '<div style="padding: 20px; text-align: center; color: #666;">Three.js library not loaded. Add <script src="https://unpkg.com/three@0.147.0/build/three.min.js"></script> to your HTML.</div>';
|
|
3749
|
+
return;
|
|
3750
|
+
}
|
|
3751
|
+
|
|
3752
|
+
try {
|
|
3753
|
+
const THREE = window.THREE;
|
|
3754
|
+
|
|
3755
|
+
// Create scene
|
|
3756
|
+
const scene = new THREE.Scene();
|
|
3757
|
+
scene.background = new THREE.Color(0xf0f0f0);
|
|
3758
|
+
|
|
3759
|
+
// Create camera
|
|
3760
|
+
const camera = new THREE.PerspectiveCamera(75, element.clientWidth / 400, 0.1, 1000);
|
|
3761
|
+
|
|
3762
|
+
// Create renderer
|
|
3763
|
+
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
3764
|
+
renderer.setSize(element.clientWidth, 400);
|
|
3765
|
+
element.innerHTML = '';
|
|
3766
|
+
element.appendChild(renderer.domElement);
|
|
3767
|
+
|
|
3768
|
+
// Store Three.js references for copy functionality (like squibview)
|
|
3769
|
+
element._threeScene = scene;
|
|
3770
|
+
element._threeCamera = camera;
|
|
3771
|
+
element._threeRenderer = renderer;
|
|
3772
|
+
|
|
3773
|
+
// Parse STL data (ASCII format)
|
|
3774
|
+
const geometry = this.parseSTL(code);
|
|
3775
|
+
const material = new THREE.MeshLambertMaterial({ color: 0x0066ff });
|
|
3776
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
3777
|
+
scene.add(mesh);
|
|
3778
|
+
|
|
3779
|
+
// Add lighting
|
|
3780
|
+
const ambientLight = new THREE.AmbientLight(0x404040, 0.6);
|
|
3781
|
+
scene.add(ambientLight);
|
|
3782
|
+
|
|
3783
|
+
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
|
|
3784
|
+
directionalLight.position.set(1, 1, 1).normalize();
|
|
3785
|
+
scene.add(directionalLight);
|
|
3786
|
+
|
|
3787
|
+
// Position camera based on object bounds
|
|
3788
|
+
const box = new THREE.Box3().setFromObject(mesh);
|
|
3789
|
+
const center = box.getCenter(new THREE.Vector3());
|
|
3790
|
+
const size = box.getSize(new THREE.Vector3());
|
|
3791
|
+
const maxDim = Math.max(size.x, size.y, size.z);
|
|
3792
|
+
|
|
3793
|
+
camera.position.set(center.x + maxDim, center.y + maxDim, center.z + maxDim);
|
|
3794
|
+
camera.lookAt(center);
|
|
3795
|
+
|
|
3796
|
+
// Animate
|
|
3797
|
+
const animate = () => {
|
|
3798
|
+
requestAnimationFrame(animate);
|
|
3799
|
+
mesh.rotation.y += 0.01;
|
|
3800
|
+
renderer.render(scene, camera);
|
|
3801
|
+
};
|
|
3802
|
+
animate();
|
|
3803
|
+
} catch (err) {
|
|
3804
|
+
console.error('STL rendering error:', err);
|
|
3805
|
+
element.innerHTML = `<pre class="qde-error">STL error: ${this.escapeHtml(err.message)}</pre>`;
|
|
3806
|
+
}
|
|
3807
|
+
};
|
|
3808
|
+
|
|
3809
|
+
// Render after DOM update
|
|
3810
|
+
setTimeout(render3D, 0);
|
|
3811
|
+
|
|
3812
|
+
// Return placeholder with data-stl-id for copy functionality
|
|
3813
|
+
return `<div id="${id}" class="qde-stl-container" data-stl-id="${id}" data-qd-fence="\`\`\`" data-qd-lang="stl" data-qd-source="${this.escapeHtml(code)}" contenteditable="false" style="height: 400px; background: #f0f0f0; display: flex; align-items: center; justify-content: center;">Loading 3D model...</div>`;
|
|
3814
|
+
}
|
|
3815
|
+
|
|
3816
|
+
/**
|
|
3817
|
+
* Parse ASCII STL format
|
|
3818
|
+
* @param {string} stlData - The STL file content
|
|
3819
|
+
* @returns {THREE.BufferGeometry} - The parsed geometry
|
|
3820
|
+
*/
|
|
3821
|
+
parseSTL(stlData) {
|
|
3822
|
+
const THREE = window.THREE;
|
|
3823
|
+
const geometry = new THREE.BufferGeometry();
|
|
3824
|
+
const vertices = [];
|
|
3825
|
+
const normals = [];
|
|
3826
|
+
|
|
3827
|
+
const lines = stlData.split('\n');
|
|
3828
|
+
let currentNormal = null;
|
|
3829
|
+
|
|
3830
|
+
for (let line of lines) {
|
|
3831
|
+
line = line.trim();
|
|
3832
|
+
|
|
3833
|
+
if (line.startsWith('facet normal')) {
|
|
3834
|
+
const parts = line.split(/\s+/);
|
|
3835
|
+
currentNormal = [parseFloat(parts[2]), parseFloat(parts[3]), parseFloat(parts[4])];
|
|
3836
|
+
} else if (line.startsWith('vertex')) {
|
|
3837
|
+
const parts = line.split(/\s+/);
|
|
3838
|
+
vertices.push(parseFloat(parts[1]), parseFloat(parts[2]), parseFloat(parts[3]));
|
|
3839
|
+
if (currentNormal) {
|
|
3840
|
+
normals.push(currentNormal[0], currentNormal[1], currentNormal[2]);
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
}
|
|
3844
|
+
|
|
3845
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
|
|
3846
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
|
|
3847
|
+
|
|
3848
|
+
return geometry;
|
|
3849
|
+
}
|
|
3850
|
+
|
|
2091
3851
|
/**
|
|
2092
3852
|
* Render Mermaid diagram
|
|
2093
3853
|
*/
|
|
@@ -2255,7 +4015,7 @@
|
|
|
2255
4015
|
this.options.lazy_linefeeds = enabled;
|
|
2256
4016
|
// Re-render if we have content
|
|
2257
4017
|
if (this._markdown) {
|
|
2258
|
-
this.
|
|
4018
|
+
this.updateFromMarkdown(this._markdown);
|
|
2259
4019
|
}
|
|
2260
4020
|
}
|
|
2261
4021
|
|
|
@@ -2326,6 +4086,9 @@
|
|
|
2326
4086
|
case 'copy-html':
|
|
2327
4087
|
this.copy('html');
|
|
2328
4088
|
break;
|
|
4089
|
+
case 'copy-rendered':
|
|
4090
|
+
this.copyRendered();
|
|
4091
|
+
break;
|
|
2329
4092
|
case 'remove-hr':
|
|
2330
4093
|
this.removeHR();
|
|
2331
4094
|
break;
|
|
@@ -2445,6 +4208,28 @@
|
|
|
2445
4208
|
}
|
|
2446
4209
|
}
|
|
2447
4210
|
|
|
4211
|
+
/**
|
|
4212
|
+
* Copy rendered content as rich text
|
|
4213
|
+
*/
|
|
4214
|
+
async copyRendered() {
|
|
4215
|
+
try {
|
|
4216
|
+
const result = await getRenderedContent(this.previewPanel);
|
|
4217
|
+
if (result.success) {
|
|
4218
|
+
// Visual feedback
|
|
4219
|
+
const btn = this.toolbar?.querySelector('[data-action="copy-rendered"]');
|
|
4220
|
+
if (btn) {
|
|
4221
|
+
const originalText = btn.textContent;
|
|
4222
|
+
btn.textContent = 'Copied!';
|
|
4223
|
+
setTimeout(() => {
|
|
4224
|
+
btn.textContent = originalText;
|
|
4225
|
+
}, 1500);
|
|
4226
|
+
}
|
|
4227
|
+
}
|
|
4228
|
+
} catch (err) {
|
|
4229
|
+
console.error('Failed to copy rendered content:', err);
|
|
4230
|
+
}
|
|
4231
|
+
}
|
|
4232
|
+
|
|
2448
4233
|
/**
|
|
2449
4234
|
* Destroy the editor
|
|
2450
4235
|
*/
|