刘光辉
12 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
export interface DropdownOption {
  display: string;
  value: string;
}
 
export interface InsertSpec {
  alias: string;
  color?: { B: number; G: number; R: number };
  controlType: 'checkbox' | 'date' | 'dropdown' | 'multiline';
  defaultChecked: boolean;
  defaultValue: string;
  options: DropdownOption[];
  placeholder: string;
  selectedIndex: number;
  tag: string;
}
 
export interface TextDefaultSpec {
  tag: string;
  value: string;
}
 
export interface TextElement {
  text: string;
}
 
export interface FixtureControl {
  AddElement?: (value: TextElement, index: number) => boolean;
  GetRange?: () => { GetText?: () => string };
  GetTag: () => string;
  RemoveAllElements?: () => boolean;
  SetAlias?: (value: string) => boolean;
  SetAppearance?: (value: string) => void;
  SetColor?: (value: RgbColor) => boolean;
  SetDate?: (value: Date) => boolean;
  SetLock?: (value: string) => boolean;
  SetPlaceholderText?: (value: string) => boolean;
  SetTag?: (value: string) => boolean;
  alias?: string;
  appearance?: string;
  color?: RgbColor;
  dateValue?: Date;
  elements: TextElement[];
  kind: string;
  lock?: string;
  placeholder?: string;
  tag: string;
}
 
export interface FixtureParagraph extends TextElement {
  AddInlineLvlSdt: (value: FixtureControl) => void;
  AddText: (value: string) => void;
  inlineControl: FixtureControl | null;
}
 
export interface FixtureRun extends TextElement {
  AddText?: (value: string) => boolean;
}
 
export interface RgbColor {
  b: number;
  g: number;
  r: number;
}
 
export interface FixtureDocument {
  GetAllContentControls: () => FixtureControl[];
  InsertContent?: (items: Array<FixtureControl | FixtureParagraph>, replaceSelection: boolean) => boolean;
  SetTrackRevisions?: (enabled: boolean) => boolean;
  controls: FixtureControl[];
}
 
export interface FixtureApi {
  CreateBlockLvlSdt?: () => FixtureControl;
  CreateCheckBoxContentControl?: (properties: { checked: boolean; checkedSymbol: number; uncheckedSymbol: number }) => FixtureControl;
  CreateComboBoxContentControl?: (options: DropdownOption[], selectedIndex: number) => FixtureControl;
  CreateDatePickerContentControl?: (properties: { format: string; lang: string }) => FixtureControl;
  CreateParagraph: () => FixtureParagraph;
  CreateRGBColor?: (r: number, g: number, b: number) => RgbColor;
  CreateRun: () => FixtureRun;
  GetDocument: () => FixtureDocument;
}
 
export interface FixtureCalls {
  checkbox: Array<{ checked: boolean; checkedSymbol: number; uncheckedSymbol: number }>;
  colors: RgbColor[];
  combo: Array<[DropdownOption[], number]>;
  date: Date[];
  dateProperties?: { format: string; lang: string };
  inserted: Array<{ block: boolean; control: FixtureControl; replaceSelection: boolean }>;
  paragraphTexts: string[];
}
 
export interface ApiFixture {
  Api: FixtureApi;
  calls: FixtureCalls;
  document: FixtureDocument;
  makeControl: (kind: string, tag?: string) => FixtureControl;
}
 
export function makeApiFixture(): ApiFixture {
  const calls: FixtureCalls = {
    combo: [],
    checkbox: [],
    date: [],
    colors: [],
    inserted: [],
    paragraphTexts: [],
  };
 
  function makeControl(kind: string, tag = ''): FixtureControl {
    const control: FixtureControl = {
      kind,
      tag,
      elements: [],
      SetTag(value) {
        control.tag = value;
        return true;
      },
      GetTag() {
        return control.tag;
      },
      SetAlias(value) {
        control.alias = value;
        return true;
      },
      SetLock(value) {
        control.lock = value;
        return true;
      },
      SetAppearance(value) {
        control.appearance = value;
      },
      SetColor(value) {
        control.color = value;
        return true;
      },
      SetPlaceholderText(value) {
        control.placeholder = value;
        return true;
      },
      SetDate(value) {
        control.dateValue = value;
        calls.date.push(value);
        return true;
      },
      RemoveAllElements() {
        control.elements = [];
        return true;
      },
      AddElement(value, index) {
        control.elements.splice(index, 0, value);
        return true;
      },
      GetRange() {
        return {
          GetText: () => control.elements.map((element) => element.text || '').join(''),
        };
      },
    };
 
    return control;
  }
 
  const document: FixtureDocument = {
    controls: [],
    GetAllContentControls() {
      return document.controls;
    },
    InsertContent(items, replaceSelection) {
      const item = items[0];
      const block = Boolean(item && 'kind' in item && item.kind === 'multiline');
      const control = block ? (item as FixtureControl) : (item as FixtureParagraph).inlineControl;
 
      if (!control) {
        return false;
      }
 
      document.controls.push(control);
      calls.inserted.push({ block, replaceSelection, control });
      return true;
    },
  };
 
  const Api: FixtureApi = {
    GetDocument() {
      return document;
    },
    CreateRGBColor(r, g, b) {
      const color = { r, g, b };
      calls.colors.push(color);
      return color;
    },
    CreateComboBoxContentControl(options, selectedIndex) {
      calls.combo.push([options, selectedIndex]);
      return makeControl('dropdown');
    },
    CreateCheckBoxContentControl(properties) {
      calls.checkbox.push(properties);
      return makeControl('checkbox');
    },
    CreateDatePickerContentControl(properties) {
      calls.dateProperties = properties;
      return makeControl('date');
    },
    CreateBlockLvlSdt() {
      return makeControl('multiline');
    },
    CreateParagraph() {
      const paragraph: FixtureParagraph = {
        text: '',
        inlineControl: null,
        AddText(value) {
          paragraph.text += value;
          calls.paragraphTexts.push(value);
        },
        AddInlineLvlSdt(value) {
          paragraph.inlineControl = value;
        },
      };
      return paragraph;
    },
    CreateRun() {
      const run: FixtureRun = {
        text: '',
        AddText(value) {
          run.text += value;
          return true;
        },
      };
      return run;
    },
  };
 
  return { Api, calls, document, makeControl };
}
 
export function makeInsertSpec(overrides: Partial<InsertSpec> & Pick<InsertSpec, 'controlType'>): InsertSpec {
  return {
    tag: 'eln.field.result',
    alias: '结果',
    placeholder: '请选择',
    defaultValue: '',
    defaultChecked: false,
    options: [],
    selectedIndex: -1,
    color: { R: 23, G: 109, B: 92 },
    ...overrides,
  };
}
 
export function installOnlyOfficeGlobals(Api: FixtureApi, scope: Record<string, unknown>): void {
  Object.assign(globalThis, { Api, Asc: { scope } });
}