UNPKG

ecmarkup

Version:

Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.

82 lines (81 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Builder_1 = require("./Builder"); class Note extends Builder_1.default { constructor(spec, node, clause) { super(spec, node); this.clause = clause; if (this.node.hasAttribute('type')) { this.type = this.node.getAttribute('type'); } else { this.type = 'normal'; } if (this.node.hasAttribute('id')) { this.id = node.getAttribute('id'); } } static async enter({ spec, node, clauseStack }) { const clause = clauseStack[clauseStack.length - 1]; // TODO reconsider if (!clause) return; // do nothing with top-level note const note = new Note(spec, node, clause); if (note.type === 'editor') { clause.editorNotes.push(note); } else { clause.notes.push(note); } } build(number) { if (this.id) { // biblio is added during the build step as we don't know // the number at build time. Could probably be fixed. this.spec.biblio.add({ type: 'note', id: this.id, number: number || 1, clauseId: this.clause.id, }); } const noteContentContainer = this.spec.doc.createElement('div'); noteContentContainer.setAttribute('class', 'note-contents'); while (this.node.childNodes.length > 0) { noteContentContainer.appendChild(this.node.childNodes[0]); } this.node.appendChild(noteContentContainer); const noteSpan = this.spec.doc.createElement('span'); noteSpan.setAttribute('class', 'note'); let label = ''; if (this.type === 'normal') { label = 'Note'; } else if (this.type === 'editor') { label = "Editor's Note"; } else { this.spec.warn({ type: 'attr-value', attr: 'type', ruleId: 'invalid-note', message: `unknown note type ${JSON.stringify(this.type)}`, node: this.node, }); } if (number !== undefined) { label += ' ' + number; } if (this.id) { // create link to note noteSpan.innerHTML = `<a href='#${this.id}'>${label}</a>`; } else { // just text noteSpan.textContent = label; } this.node.insertBefore(noteSpan, noteContentContainer); } } Note.elements = ['EMU-NOTE']; exports.default = Note;