| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import {
- Paragraph,
- INumberingOptions,
- LevelFormat,
- Table,
- } from "docx"
- import { handleNestElement } from "./handleElement"
- import { handleNodes } from "./handleNodes"
- const config: {numbering?: INumberingOptions} = {
- numbering: {
- config: [
- {
- reference: "bullet-numbering", // 自定义编号 ID
- levels: [
- {
- level: 0,
- format: LevelFormat.BULLET, // 无序列表用 BULLET
- text: "•", // 符号
- alignment: "left",
- style: {
- paragraph: {
- indent: { left: 720, hanging: 260 }
- }
- }
- },
- {
- level: 1,
- format: LevelFormat.BULLET,
- text: "o",
- alignment: "left",
- style: {
- paragraph: {
- indent: { left: 1440, hanging: 260 }
- }
- }
- },
- {
- level: 2,
- format: LevelFormat.BULLET,
- text: "▪",
- alignment: "left",
- style: {
- paragraph: { indent: { left: 2160, hanging: 260 } }
- }
- }
- ]
- },
- {
- reference: "ordered-numbering", // 有序列表 ID
- levels: [
- {
- level: 0,
- format: LevelFormat.DECIMAL, // 有序列表用 DECIMAL(1, 2, 3)
- text: "%1.",
- alignment: "left",
- style: {
- paragraph: {
- indent: { left: 720, hanging: 260 }
- }
- }
- },
- {
- level: 1,
- format: LevelFormat.LOWER_LETTER, // 二级有序列表 a,b,c
- text: "%2.",
- alignment: "left",
- style: {
- paragraph: {
- indent: { left: 1440, hanging: 260 }
- }
- }
- },
- {
- level: 2,
- format: LevelFormat.LOWER_ROMAN, // i, ii, iii ...
- text: "%3.",
- alignment: "left",
- style: {
- paragraph: { indent: { left: 2160, hanging: 260 } }
- }
- }
- ]
- }
- ]
- }
- }
- const handleList = async (el: Element): Promise<(Paragraph | Table)[] | undefined> => {
- const tag = el.tagName.toLowerCase()
- // 无序列表
- if (tag === "ul") {
- const paragraphList: (Paragraph | Table)[] = []
- for (const li of Array.from(el.querySelectorAll("li"))) {
- if (li.children.length === 0) {
- // 普通文本结构
- const paragraph = new Paragraph({
- text: li.textContent ?? "",
- numbering: {
- reference: "bullet-numbering",
- level: 0,
- },
- bullet: { level: 0 },
- spacing: { after: 100 },
- })
- paragraphList.push(paragraph)
- } else {
- // 复杂文本,交给handleNodes统一处理
- paragraphList.push(handleNodes(li.childNodes))
- }
- const childParagraph = await handleNestElement(li)
- paragraphList.push(...childParagraph)
- }
- return paragraphList
- }
- // 有序列表
- if (tag === "ol") {
- const paragraphList: (Paragraph | Table)[] = []
- for (const li of Array.from(el.querySelectorAll("li"))) {
- if (li.children.length === 0) {
- const paragraph = new Paragraph({
- text: li.textContent ?? "",
- numbering: {
- reference: "ordered-numbering",
- level: 0,
- },
- spacing: { after: 100 },
- })
- paragraphList.push(paragraph)
- } else {
- // 复杂文本,交给handleNodes统一处理
- paragraphList.push(handleNodes(li.childNodes))
- }
- const childParagraph = await handleNestElement(li)
- paragraphList.push(...childParagraph)
- }
- return paragraphList
- }
- }
- export default {
- handleList,
- config
- }
|