import React from "react"; import * as std from "../../law/std"; import type { SentenceChildEL } from "../../node/cst/inline"; import { isSentenceChildEL } from "../../node/cst/inline"; import type { EL } from "../../node/el"; import { NotImplementedError } from "../../util"; import { DOCXAmendProvision, HTMLAmendProvision } from "./amendProvision"; import { DOCXAppdxItem, HTMLAppdxItem } from "./appdxItem"; import { DOCXArticle, HTMLArticle } from "./article"; import { DOCXArticleGroup, HTMLArticleGroup } from "./articleGroup"; import { DOCXColumnsOrSentencesRun, HTMLColumnsOrSentencesRun } from "./columnsOrSentencesRun"; import type { DOCXComponentProps } from "../common/docx/component"; import { wrapDOCXComponent } from "../common/docx/component"; import { w } from "../common/docx/tags"; import { DOCXFigRun, HTMLFigRun } from "./figRun"; import type { HTMLComponentProps } from "../common/html"; import { wrapHTMLComponent } from "../common/html"; import { DOCXItemStruct, HTMLItemStruct } from "./itemStruct"; import { DOCXEnactStatement, DOCXLaw, DOCXPreamble, HTMLEnactStatement, HTMLLaw, HTMLPreamble } from "./law"; import { DOCXList, HTMLList } from "./list"; import { DOCXNoteLike, HTMLNoteLike } from "./noteLike"; import { DOCXParagraphItem, HTMLParagraphItem } from "./paragraphItem"; import { DOCXRemarks, HTMLRemarks } from "./remarks"; import { DOCXSentenceChildrenRun, HTMLSentenceChildrenRun } from "./sentenceChildrenRun"; import { DOCXSupplNote, HTMLSupplNote } from "./supplNote"; import { DOCXTable, HTMLTable } from "./table"; import { DOCXTOC, DOCXTOCItem, HTMLTOC, HTMLTOCItem } from "./toc"; import { withKey } from "../common"; export interface AnyELsProps { els: (std.StdEL | std.__EL | string)[], indent: number, } export const HTMLAnyELsCSS = /*css*/` `; export const HTMLAnyELsToBlocks = (props: HTMLComponentProps & AnyELsProps): (React.JSX.Element[] | React.JSX.Element)[] => { const { els, htmlOptions, indent } = props; const blocks: (React.JSX.Element[] | React.JSX.Element)[] = []; let currentRuns: React.JSX.Element[] = []; const flushRuns = () => { if (currentRuns.length > 0) { blocks.push(currentRuns); currentRuns = []; } }; for (let i = 0; i < els.length; i++) { const el = els[i]; if (std.isLaw(el)) { flushRuns(); blocks.push(); } else if (std.isArticleGroup(el) || std.isMainProvision(el) || std.isSupplProvision(el)) { flushRuns(); blocks.push(); } else if (std.isArticle(el)) { flushRuns(); blocks.push(); } else if (std.isParagraphItem(el)) { flushRuns(); blocks.push(); } else if (std.isTable(el)) { flushRuns(); blocks.push(); } else if (std.isTableStruct(el) || std.isFigStruct(el) || std.isNoteLikeStruct(el)) { flushRuns(); blocks.push(); } else if (std.isAppdxItem(el)) { flushRuns(); blocks.push(); } else if (std.isRemarks(el)) { flushRuns(); blocks.push(); } else if (std.isNoteLike(el)) { flushRuns(); blocks.push(); } else if (std.isList(el)) { flushRuns(); blocks.push(); } else if (std.isAmendProvision(el)) { flushRuns(); blocks.push(); } else if (std.isSupplNote(el)) { flushRuns(); blocks.push(); } else if (std.isEnactStatement(el)) { flushRuns(); blocks.push(); } else if (std.isPreamble(el)) { flushRuns(); blocks.push(); } else if (std.isTOC(el)) { flushRuns(); blocks.push(); } else if (std.isTOCItem(el)) { flushRuns(); blocks.push(); } else if (std.isArticleGroupTitle(el) || std.isLawTitle(el)) { flushRuns(); blocks.push((
)); } else if (std.isSentence(el)) { let j = i + 1; while (j < els.length && std.isSentence(els[j])) j++; const sentences = els.slice(i, j) as std.Sentence[]; currentRuns.push(); i = j - 1; } else if (std.isColumn(el)) { let j = i + 1; while (j < els.length && std.isColumn(els[j])) j++; const columns = els.slice(i, j) as std.Column[]; currentRuns.push(); i = j - 1; } else if (typeof el === "string" || isSentenceChildEL(el)) { // "Line", "QuoteStruct", "ArithFormula", "Ruby", "Sup", "Sub" let j = i + 1; while (j < els.length && (typeof els[j] === "string" || isSentenceChildEL(els[j] as EL))) j++; const sentenceChildren = els.slice(i, j) as (SentenceChildEL | string)[]; currentRuns.push(); i = j - 1; } else if (std.isFig(el)) { currentRuns.push(); } else { throw new NotImplementedError(el.tag); } } flushRuns(); return blocks; }; export const HTMLAnyELs = wrapHTMLComponent("HTMLAnyELs", ((props: HTMLComponentProps & AnyELsProps) => { const { indent } = props; const rawBlocks = HTMLAnyELsToBlocks(props); const blocks: React.JSX.Element[] = []; for (const rawBlock of rawBlocks) { if (Array.isArray(rawBlock)) { blocks.push((
{withKey(rawBlock)}
)); } else { blocks.push(rawBlock); } } return (<> {withKey(blocks)} ); })); export const DOCXAnyELsToBlocks = (props: DOCXComponentProps & AnyELsProps): (React.JSX.Element[] | React.JSX.Element)[] => { const { els, docxOptions, indent } = props; const blocks: (React.JSX.Element[] | React.JSX.Element)[] = []; let currentRuns: React.JSX.Element[] = []; const flushRuns = () => { if (currentRuns.length > 0) { blocks.push(currentRuns); currentRuns = []; } }; for (let i = 0; i < els.length; i++) { const el = els[i]; if (std.isLaw(el)) { flushRuns(); blocks.push(); } else if (std.isArticleGroup(el) || std.isMainProvision(el) || std.isSupplProvision(el)) { flushRuns(); blocks.push(); } else if (std.isArticle(el)) { flushRuns(); blocks.push(); } else if (std.isParagraphItem(el)) { flushRuns(); blocks.push(); } else if (std.isTable(el)) { flushRuns(); blocks.push(); } else if (std.isTableStruct(el) || std.isFigStruct(el) || std.isNoteLikeStruct(el)) { flushRuns(); blocks.push(); } else if (std.isAppdxItem(el)) { flushRuns(); blocks.push(); } else if (std.isRemarks(el)) { flushRuns(); blocks.push(); } else if (std.isNoteLike(el)) { flushRuns(); blocks.push(); } else if (std.isList(el)) { flushRuns(); blocks.push(); } else if (std.isAmendProvision(el)) { flushRuns(); blocks.push(); } else if (std.isSupplNote(el)) { flushRuns(); blocks.push(); } else if (std.isEnactStatement(el)) { flushRuns(); blocks.push(); } else if (std.isPreamble(el)) { flushRuns(); blocks.push(); } else if (std.isTOC(el)) { flushRuns(); blocks.push(); } else if (std.isTOCItem(el)) { flushRuns(); blocks.push(); } else if (std.isArticleGroupTitle(el) || std.isLawTitle(el)) { flushRuns(); blocks.push(( )); } else if (std.isSentence(el)) { let j = i + 1; while (j < els.length && std.isSentence(els[j])) j++; const sentences = els.slice(i, j) as std.Sentence[]; currentRuns.push(); i = j - 1; } else if (std.isColumn(el)) { let j = i + 1; while (j < els.length && std.isColumn(els[j])) j++; const columns = els.slice(i, j) as std.Column[]; currentRuns.push(); i = j - 1; } else if (typeof el === "string" || isSentenceChildEL(el)) { // "Line", "QuoteStruct", "ArithFormula", "Ruby", "Sup", "Sub" let j = i + 1; while (j < els.length && (typeof els[j] === "string" || isSentenceChildEL(els[j] as EL))) j++; const sentenceChildren = els.slice(i, j) as (SentenceChildEL | string)[]; currentRuns.push(); i = j - 1; } else if (std.isFig(el)) { currentRuns.push(); } else { throw new NotImplementedError(el.tag); } } flushRuns(); return blocks; }; export const DOCXAnyELs = wrapDOCXComponent("DOCXAnyELs", ((props: DOCXComponentProps & AnyELsProps) => { const { indent } = props; const rawBlocks = DOCXAnyELsToBlocks(props); const blocks: React.JSX.Element[] = []; for (const rawBlock of rawBlocks) { if (Array.isArray(rawBlock)) { blocks.push(( {withKey(rawBlock)} )); } else { blocks.push(rawBlock); } } return (<> {withKey(blocks)} ); }));