UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const Builder_1 = require("./Builder");
13/*@internal*/
14class Note extends Builder_1.default {
15 constructor(spec, node, clause) {
16 super(spec, node);
17 this.clause = clause;
18 if (this.node.hasAttribute('type')) {
19 this.type = this.node.getAttribute('type');
20 }
21 else {
22 this.type = 'normal';
23 }
24 if (this.node.hasAttribute('id')) {
25 this.id = node.getAttribute('id');
26 }
27 }
28 static enter({ spec, node, clauseStack }) {
29 return __awaiter(this, void 0, void 0, function* () {
30 const clause = clauseStack[clauseStack.length - 1];
31 // TODO reconsider
32 if (!clause)
33 return; // do nothing with top-level note
34 const note = new Note(spec, node, clause);
35 if (note.type === 'editor') {
36 clause.editorNotes.push(note);
37 }
38 else {
39 clause.notes.push(note);
40 }
41 });
42 }
43 build(number) {
44 if (this.id) {
45 // biblio is added during the build step as we don't know
46 // the number at build time. Could probably be fixed.
47 this.spec.biblio.add({
48 type: 'note',
49 id: this.id,
50 node: this.node,
51 number: number || 1,
52 clauseId: this.clause.id,
53 });
54 }
55 const noteContentContainer = this.spec.doc.createElement('div');
56 noteContentContainer.setAttribute('class', 'note-contents');
57 while (this.node.childNodes.length > 0) {
58 noteContentContainer.appendChild(this.node.childNodes[0]);
59 }
60 this.node.appendChild(noteContentContainer);
61 const noteSpan = this.spec.doc.createElement('span');
62 noteSpan.setAttribute('class', 'note');
63 let label = '';
64 if (this.type === 'normal') {
65 label = 'Note';
66 }
67 else if (this.type === 'editor') {
68 label = "Editor's Note";
69 }
70 else {
71 this.spec.warn({
72 type: 'attr-value',
73 attr: 'type',
74 ruleId: 'invalid-note',
75 message: `unknown note type ${JSON.stringify(this.type)}`,
76 node: this.node,
77 });
78 }
79 if (number !== undefined) {
80 label += ' ' + number;
81 }
82 if (this.id) {
83 // create link to note
84 noteSpan.innerHTML = `<a href='#${this.id}'>${label}</a>`;
85 }
86 else {
87 // just text
88 noteSpan.textContent = label;
89 }
90 this.node.insertBefore(noteSpan, noteContentContainer);
91 }
92}
93exports.default = Note;
94Note.elements = ['EMU-NOTE'];