Parse pasted prompts into timeline shots
This commit is contained in:
@@ -123,8 +123,42 @@ function configure(node) {
|
||||
if (typeof clip.prompt === "string" && clip.prompt.length) return clip.prompt;
|
||||
return formatPromptFromSlots(clip);
|
||||
};
|
||||
const parseTime = (value) => {
|
||||
const parts = String(value || "").split(":").map(Number);
|
||||
if (parts.some((part) => !Number.isFinite(part))) return NaN;
|
||||
if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
||||
if (parts.length === 2) return parts[0] * 60 + parts[1];
|
||||
return parts[0];
|
||||
};
|
||||
const parsePromptSlots = (clip, value) => {
|
||||
const text = String(value || "");
|
||||
const markerPattern = /\[Shot\s+\d+\]\s+At\s+((?:\d+(?::\d+){1,2}(?:\.\d+)?|\d+(?:\.\d+)?))\s*,\s*/gi;
|
||||
const markers = [...text.matchAll(markerPattern)];
|
||||
if (!markers.length) return false;
|
||||
const duration = Math.max(0.1, Number(clip.duration) || 0.1);
|
||||
const slots = markers.map((marker, index) => {
|
||||
const rawStart = parseTime(marker[1]);
|
||||
const start = Number.isFinite(rawStart)
|
||||
? Math.min(Math.max(0, rawStart), Math.max(0, duration - 0.1))
|
||||
: 0;
|
||||
const next = markers[index + 1];
|
||||
const rawEnd = next ? parseTime(next[1]) : duration;
|
||||
const end = Math.min(duration, Math.max(start + 0.1, Number.isFinite(rawEnd) ? rawEnd : duration));
|
||||
const from = marker.index + marker[0].length;
|
||||
const to = next ? next.index : text.length;
|
||||
return { start, end, text: text.slice(from, to).trim() };
|
||||
});
|
||||
slots.forEach((slot, index) => {
|
||||
if (index > 0) slot.start = slots[index - 1].end;
|
||||
if (slot.end <= slot.start) slot.end = Math.min(duration, slot.start + 0.1);
|
||||
});
|
||||
slots[slots.length - 1].end = duration;
|
||||
clip.slots = slots;
|
||||
return true;
|
||||
};
|
||||
const syncPrompt = (clip, value) => {
|
||||
const text = String(value || "");
|
||||
if (parsePromptSlots(clip, text)) return true;
|
||||
// The shot description starts on the same line as the marker in the
|
||||
// CSGlide-style editor. Only consume the marker prefix through its
|
||||
// timestamp comma; consuming the whole line would discard text such
|
||||
@@ -139,6 +173,7 @@ function configure(node) {
|
||||
const to = index + 1 < marks.length ? marks[index + 1].index : text.length;
|
||||
clip.slots[slotIndex].text = text.slice(from, to).trim();
|
||||
});
|
||||
return false;
|
||||
};
|
||||
const retimePrompt = (text, clip) => {
|
||||
let index = 0;
|
||||
@@ -434,6 +469,17 @@ function configure(node) {
|
||||
autocomplete = menu;
|
||||
};
|
||||
prompt.oninput = () => { rememberPromptSelection(); promptText = prompt.value; clip.prompt = prompt.value; };
|
||||
prompt.onpaste = () => {
|
||||
// Let the browser insert the pasted text first, then parse its
|
||||
// shot markers and redraw the track without losing the paste.
|
||||
setTimeout(() => {
|
||||
const changed = syncPrompt(clip, prompt.value);
|
||||
promptText = prompt.value;
|
||||
clip.prompt = prompt.value;
|
||||
persistState();
|
||||
if (changed) render();
|
||||
}, 0);
|
||||
};
|
||||
prompt.onselect = rememberPromptSelection;
|
||||
prompt.onkeyup = rememberPromptSelection;
|
||||
prompt.onmouseup = rememberPromptSelection;
|
||||
|
||||
Reference in New Issue
Block a user