Files
youtube-generator/src/engine/limits.ts
T
poli0981andClaude Opus 5.5 3c985a07d6 feat(editor): quick start, next part, store links pasted anywhere, more stores and platforms
Editor:
- Quick start at the top: pick the channel profile and the game preset
  (remembered per device); replacing fields that hold something else asks
  first, everything offers Undo; "Update profile / preset" appears when the
  editor drifts from the one in use.
- Next part (number + 1) and New video (same game) clear what belonged to
  the last video: boss, timestamps, endings, pinned comment, sponsor…
  Applying a preset for another game or a template does the same first.
- Start over replaces Clear Draft and keeps the channel (profile fields);
  a new draft starts in the Default Output Language (it was always "en").
- A store link pasted anywhere — the page, the Game name field, another
  store's field — lands in the right store field, sets the platform for a
  game's first link and names the game from it (Roman numerals and
  well-known possessives restored: "Final Fantasy VII", "Baldur's Gate 3").
  Steam links keep their name slug and drop tracking parameters.
- Game-name suggestions from presets and history.

Catalogs:
- Stores: EA app, Ubisoft, Battle.net, Google Play, App Store, Meta Quest.
- Social: YouTube, Threads, Kick, Reddit, Bilibili; "X (Twitter)" with the
  X mark; threads.net and youtu.be links accepted.
- Cross-posting: YouTube Shorts, X (counted the way X counts — CJK,
  Vietnamese letters with diacritics and emoji count twice), Threads (one
  topic tag), Bluesky; hashtag caps per platform.
- Banks: MBV, VCBNeo, Vikki Bank (renamed OceanBank, CBBank, DongA Bank —
  saved old names get a one-click switch), Cake by VPBank, Timo.

Output:
- An empty channel name no longer leaves "…of Hades on ." — the channel
  phrase is dropped per language (8 locales, every template tested).
- Title alternatives are real title shapes with the user's separator and
  badge case; "Use this shape" sets them (new Title order setting:
  game first / video first); over-long ones can't be copied.
- Copy All can include the tags (setting); Batch picks Part or Demo part
  and continues from the editor's part number; Ctrl/Cmd+S saves the
  output to History.
- Social editor headings and hints translated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 22:29:44 +07:00

128 lines
4.8 KiB
TypeScript

import type { GeneratorOutput, CharLimitWarning } from "./types";
/**
* Whether a generated output may be copied, derived from the engine's own
* over-limit warnings.
*
* `renderAll` has always returned `warnings: CharLimitWarning[]` — and until
* v0.35.0 **nothing in the UI read it.** Instead, three separate ad-hoc
* over-limit checks had grown up in the components, each with a different
* blind spot: `CopyButton` coloured itself red but stayed clickable,
* `CopyAllBar` checked title and description but not tags *and* skipped every
* check on the multi-language path, and BatchPage passed no limits at all, so
* a 6000-character description copied silently.
*
* This module is the single derivation. Pure, no React — the same rule applies
* on the Output tab and in Batch.
*
* Note the engine's `checkTitleWarning` / `checkDescriptionWarning` only fire
* when a field is *strictly over* its limit, never at the 80% "getting close"
* threshold the character counter colours yellow. So a non-empty `warnings`
* array is an exact "YouTube will reject this" signal, safe to block on.
*/
export type LimitedField = CharLimitWarning["field"];
export interface LimitOverflow {
field: LimitedField;
current: number;
limit: number;
}
export interface OutputLimitStatus {
/** True when at least one field is over its limit. */
blocked: boolean;
/** One entry per offending field, deduped, in title → description → tags order. */
overflows: readonly LimitOverflow[];
}
export const EMPTY_LIMIT_STATUS: OutputLimitStatus = { blocked: false, overflows: [] };
/** Stable display order, independent of the order warnings were pushed. */
const FIELD_ORDER: readonly LimitedField[] = ["title", "description", "tags"];
function byFieldOrder(a: LimitOverflow, b: LimitOverflow): number {
return FIELD_ORDER.indexOf(a.field) - FIELD_ORDER.indexOf(b.field);
}
/**
* Read one output's over-limit state.
*
* Deliberately ignores `CharLimitWarning.message`, which is a hardcoded
* English sentence built inside the engine and therefore unusable in a
* localized UI. The UI renders `output.limits.*` keys from the numbers instead.
*/
export function getOutputLimitStatus(output: Pick<GeneratorOutput, "warnings">): OutputLimitStatus {
if (output.warnings.length === 0) return EMPTY_LIMIT_STATUS;
const overflows = output.warnings
.map(({ field, current, limit }) => ({ field, current, limit }))
.sort(byFieldOrder);
return { blocked: true, overflows };
}
/**
* Combine several outputs into one status — the multi-language Output tabs and
* the Copy-All-Batch blob, both of which concatenate text that may come from
* any of them.
*
* Deduped by field, keeping the worst offender, so five Batch rows all over on
* description produce one banner line rather than five.
*/
export function mergeLimitStatus(statuses: readonly OutputLimitStatus[]): OutputLimitStatus {
const worst = new Map<LimitedField, LimitOverflow>();
for (const status of statuses) {
for (const overflow of status.overflows) {
const existing = worst.get(overflow.field);
if (!existing || overflow.current > existing.current) {
worst.set(overflow.field, overflow);
}
}
}
if (worst.size === 0) return EMPTY_LIMIT_STATUS;
return { blocked: true, overflows: [...worst.values()].sort(byFieldOrder) };
}
/**
* Fields YouTube will refuse because they contain "<" or ">" (Studio rejects
* both characters in titles and descriptions). A game or chapter name typed
* with them used to go through unremarked and fail on upload.
*/
export function fieldsWithForbiddenChars(
output: Pick<GeneratorOutput, "title" | "description">,
): Array<"title" | "description"> {
const fields: Array<"title" | "description"> = [];
if (/[<>]/.test(output.title)) fields.push("title");
if (/[<>]/.test(output.description)) fields.push("description");
return fields;
}
/** Whether one field of an output is over its YouTube limit. */
export function isFieldOver(status: OutputLimitStatus, field: LimitedField): boolean {
return status.overflows.some((o) => o.field === field);
}
/**
* Copy All is title + description, so it is blocked by either of those —
* but not by the tags, which it doesn't include.
*/
export function isCopyAllBlocked(status: OutputLimitStatus, withTags = false): boolean {
return (
isFieldOver(status, "title") ||
isFieldOver(status, "description") ||
(withTags && isFieldOver(status, "tags"))
);
}
/**
* What Copy All copies: the title and description, and the tags when the
* "Copy All includes tags" setting is on — each separated by a blank line.
*/
export function copyAllText(
output: { title: string; description: string; tagString: string },
withTags: boolean,
): string {
const parts = [output.title, output.description];
if (withTags && output.tagString) parts.push(output.tagString);
return parts.join("\n\n");
}