File size: 2,931 Bytes
7dc28be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { docs_v1 } from 'googleapis';
import * as GDocsHelpers from '../../googleDocsApiHelpers.js';

export interface ExtractedSmartChip {
  type: 'date' | 'richLink' | 'person';
  startIndex: number | null;
  endIndex: number | null;
  text?: string | null;
  properties: Record<string, unknown>;
}

function getContentSource(
  doc: docs_v1.Schema$Document,
  tabId?: string
): docs_v1.Schema$StructuralElement[] {
  if (tabId) {
    const targetTab = GDocsHelpers.findTabById(doc, tabId);
    if (!targetTab?.documentTab?.body?.content) return [];
    return targetTab.documentTab.body.content;
  }

  if (doc.body?.content) return doc.body.content;
  if (doc.tabs?.[0]?.documentTab?.body?.content) return doc.tabs[0].documentTab.body.content;
  return [];
}

function visitContent(
  content: docs_v1.Schema$StructuralElement[],
  out: ExtractedSmartChip[]
): void {
  for (const element of content) {
    for (const paragraphElement of element.paragraph?.elements ?? []) {
      if (paragraphElement.dateElement) {
        out.push({
          type: 'date',
          startIndex: paragraphElement.startIndex ?? null,
          endIndex: paragraphElement.endIndex ?? null,
          text: paragraphElement.dateElement.dateElementProperties?.displayText ?? null,
          properties: {
            dateId: paragraphElement.dateElement.dateId ?? null,
            ...paragraphElement.dateElement.dateElementProperties,
          },
        });
      }

      if (paragraphElement.richLink) {
        out.push({
          type: 'richLink',
          startIndex: paragraphElement.startIndex ?? null,
          endIndex: paragraphElement.endIndex ?? null,
          text: paragraphElement.richLink.richLinkProperties?.title ?? null,
          properties: {
            richLinkId: paragraphElement.richLink.richLinkId ?? null,
            ...paragraphElement.richLink.richLinkProperties,
          },
        });
      }

      if (paragraphElement.person) {
        out.push({
          type: 'person',
          startIndex: paragraphElement.startIndex ?? null,
          endIndex: paragraphElement.endIndex ?? null,
          text:
            paragraphElement.person.personProperties?.name ??
            paragraphElement.person.personProperties?.email ??
            null,
          properties: {
            personId: paragraphElement.person.personId ?? null,
            ...paragraphElement.person.personProperties,
          },
        });
      }
    }

    if (element.table?.tableRows) {
      for (const row of element.table.tableRows) {
        for (const cell of row.tableCells ?? []) {
          visitContent(cell.content ?? [], out);
        }
      }
    }
  }
}

export function extractSmartChips(
  doc: docs_v1.Schema$Document,
  tabId?: string
): ExtractedSmartChip[] {
  const content = getContentSource(doc, tabId);
  const chips: ExtractedSmartChip[] = [];
  visitContent(content, chips);
  return chips;
}