@sciflow/editor-start 0.0.1-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ /**
2
+ * @module reference-list
3
+ *
4
+ * Developer overview:
5
+ * --------------------
6
+ * `SciFlowReferenceListElement` renders a draggable, highlighted list of
7
+ * references. It replaces the bespoke demo sidebar logic so integrators can
8
+ * reuse the same markup and drag payloads in their own UIs.
9
+ */
10
+ import { __decorate } from "tslib";
11
+ import { css, html, LitElement, nothing, unsafeCSS } from 'lit';
12
+ import { customElement, property } from 'lit/decorators.js';
13
+ import { classMap } from 'lit/directives/class-map.js';
14
+ import referenceListCss from './reference-list.css?inline';
15
+ import { applyThemeStylesToRoot, subscribeToSciFlowTheme } from './theme.js';
16
+ let SciFlowReferenceListElement = class SciFlowReferenceListElement extends LitElement {
17
+ constructor() {
18
+ super(...arguments);
19
+ /** References to render. */
20
+ this.references = null;
21
+ /** Reference IDs to highlight. */
22
+ this.highlightedIds = [];
23
+ /** Text displayed when no references are available. */
24
+ this.emptyText = 'No references yet.';
25
+ this.themeStyleElements = [];
26
+ }
27
+ static { this.styles = css `${unsafeCSS(referenceListCss)}`; }
28
+ connectedCallback() {
29
+ super.connectedCallback();
30
+ this.themeUnsub = subscribeToSciFlowTheme((cssTexts) => {
31
+ this.themeStyleElements = applyThemeStylesToRoot(this.renderRoot, this.themeStyleElements, cssTexts);
32
+ });
33
+ }
34
+ disconnectedCallback() {
35
+ this.themeUnsub?.();
36
+ this.themeUnsub = undefined;
37
+ super.disconnectedCallback();
38
+ }
39
+ /**
40
+ * Highlight the supplied reference ids.
41
+ *
42
+ * @param referenceIds IDs to highlight.
43
+ */
44
+ highlight(referenceIds = []) {
45
+ this.highlightedIds = Array.from(referenceIds)
46
+ .map((id) => (typeof id === 'string' ? id.trim() : ''))
47
+ .filter((id) => Boolean(id));
48
+ }
49
+ render() {
50
+ const references = Array.isArray(this.references) ? this.references : [];
51
+ const highlightSet = new Set((this.highlightedIds ?? []).map((id) => (typeof id === 'string' ? id.trim() : '')).filter(Boolean));
52
+ if (references.length === 0) {
53
+ return html `
54
+ <ol class="reference-list">
55
+ <li class="reference-item reference-empty">${this.emptyText}</li>
56
+ </ol>
57
+ `;
58
+ }
59
+ return html `
60
+ <ol class="reference-list">
61
+ ${references.map((reference) => {
62
+ const { displayText, id } = this.formatReference(reference);
63
+ const isHighlighted = id ? highlightSet.has(id) : false;
64
+ return html `
65
+ <li
66
+ class=${classMap({
67
+ 'reference-item': true,
68
+ 'reference-highlight': isHighlighted,
69
+ })}
70
+ data-reference-id=${id ?? ''}
71
+ draggable="true"
72
+ @dragstart=${(event) => this.handleDragStart(event, reference, displayText)}
73
+ >
74
+ <span class="reference-text">${displayText || nothing}</span>
75
+ <span class="reference-meta">
76
+ ${id
77
+ ? html `<code class="reference-id" aria-label=${`CSL id ${id}`}>${id}</code>`
78
+ : nothing}
79
+ <span class="reference-drag" aria-hidden="true">drag</span>
80
+ </span>
81
+ </li>
82
+ `;
83
+ })}
84
+ </ol>
85
+ `;
86
+ }
87
+ formatReference(reference) {
88
+ const id = typeof reference.id === 'string' ? reference.id : null;
89
+ const rawCitation = typeof reference.rawCitation === 'string'
90
+ ? reference.rawCitation
91
+ : typeof reference.raw_citation === 'string'
92
+ ? reference.raw_citation
93
+ : null;
94
+ if (rawCitation && rawCitation.trim()) {
95
+ return { id, displayText: rawCitation.trim() };
96
+ }
97
+ const authors = Array.isArray(reference.author) && reference.author.length > 0
98
+ ? reference.author
99
+ .map((entry) => this.formatAuthor(entry))
100
+ .filter((name) => Boolean(name))
101
+ : [];
102
+ const issued = reference.issued?.['date-parts']?.[0]?.[0] ?? null;
103
+ const parts = [
104
+ authors.join('; '),
105
+ reference.title ?? '',
106
+ issued ? `(${issued})` : '',
107
+ reference.publisher ?? reference['container-title'] ?? '',
108
+ reference['publisher-place'] ?? '',
109
+ ]
110
+ .map((value) => value?.toString().trim())
111
+ .filter(Boolean);
112
+ return { id, displayText: parts.join('. ') };
113
+ }
114
+ formatAuthor(entry) {
115
+ if (!entry) {
116
+ return null;
117
+ }
118
+ const family = entry.family ?? '';
119
+ const given = entry.given ? `, ${entry.given}` : '';
120
+ const name = `${family}${given}`.trim();
121
+ return name || null;
122
+ }
123
+ handleDragStart(event, reference, displayText) {
124
+ const transfer = event.dataTransfer;
125
+ if (!transfer) {
126
+ return;
127
+ }
128
+ transfer.setData('application/x-sciflow-reference', 'sidebar');
129
+ transfer.effectAllowed = 'copy';
130
+ const payload = {
131
+ type: 'reference',
132
+ id: reference.id ?? null,
133
+ text: displayText,
134
+ reference,
135
+ };
136
+ try {
137
+ transfer.setData('application/json', JSON.stringify(payload));
138
+ }
139
+ catch {
140
+ // Ignore JSON serialization errors.
141
+ }
142
+ const dropText = reference.id ? `[${reference.id}]` : displayText || '';
143
+ if (dropText) {
144
+ transfer.setData('text/plain', dropText);
145
+ }
146
+ }
147
+ };
148
+ __decorate([
149
+ property({ attribute: false })
150
+ ], SciFlowReferenceListElement.prototype, "references", void 0);
151
+ __decorate([
152
+ property({ attribute: false })
153
+ ], SciFlowReferenceListElement.prototype, "highlightedIds", void 0);
154
+ __decorate([
155
+ property({ type: String, attribute: 'empty-text' })
156
+ ], SciFlowReferenceListElement.prototype, "emptyText", void 0);
157
+ SciFlowReferenceListElement = __decorate([
158
+ customElement('sciflow-reference-list')
159
+ ], SciFlowReferenceListElement);
160
+ export { SciFlowReferenceListElement };
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @module selection-editor
3
+ *
4
+ * Developer overview:
5
+ * --------------------
6
+ * `SciFlowSelectionEditorElement` is a companion component for `sciflow-editor`
7
+ * that displays and allows editing of the currently selected element's attributes.
8
+ *
9
+ * It demonstrates how to:
10
+ * - Listen to editor selection changes
11
+ * - Extract node information from the document
12
+ * - Use the command API to update node attributes
13
+ * - Build reactive UI that stays in sync with editor state
14
+ */
15
+ import { LitElement } from 'lit';
16
+ import type { SciFlowEditorElement } from './editor-element.js';
17
+ import { type CitationSourceEditorAdapter } from './citation-source-adapter.js';
18
+ /**
19
+ * Custom element that displays and edits the selected element's attributes.
20
+ *
21
+ * Responsibilities:
22
+ * - Resolve the nearest `sciflow-editor` instance (or accept one via property)
23
+ * - Listen to selection changes and extract node information
24
+ * - Display node type, ID, and attributes
25
+ * - Allow editing of attributes through the command API
26
+ */
27
+ export declare class SciFlowSelectionEditorElement extends LitElement {
28
+ /** Reference to the editor element. May be passed directly via property binding. */
29
+ editor: SciFlowEditorElement | null;
30
+ /** ID reference to resolve an editor instance declaratively. */
31
+ for?: string;
32
+ /** Custom adapter for editing citation source. When unset, uses the global adapter. */
33
+ citationSourceAdapter: CitationSourceEditorAdapter | null;
34
+ private elementInfo;
35
+ private resolvedEditor;
36
+ private selectionListener?;
37
+ private readyListener?;
38
+ private themeUnsub?;
39
+ private themeStyleElements;
40
+ private citationSourceContainer;
41
+ static styles: import("lit").CSSResult;
42
+ connectedCallback(): void;
43
+ disconnectedCallback(): void;
44
+ updated(changedProperties: Map<string | number | symbol, unknown>): void;
45
+ protected render(): import("lit-html").TemplateResult<1>;
46
+ /**
47
+ * Find the target editor element and attach listeners for selection changes.
48
+ */
49
+ private resolveEditorReference;
50
+ private resolveEditor;
51
+ /** Clean up listeners attached to the last editor instance. */
52
+ private detachEditorListeners;
53
+ /**
54
+ * Extract element information from the current selection.
55
+ */
56
+ private updateElementInfo;
57
+ /** Convenience getter mirroring `editor.getCommands()`. */
58
+ private getCommandRunner;
59
+ /**
60
+ * Render a single attribute field, decoding known encoded values for readability.
61
+ */
62
+ private renderAttributeField;
63
+ /**
64
+ * Decode known encoded attributes for more readable display/editing.
65
+ */
66
+ private decodeSpecialAttribute;
67
+ /**
68
+ * Mount the citation source adapter UI when a citation node is selected.
69
+ */
70
+ private mountCitationSourceAdapter;
71
+ /**
72
+ * Unmount the citation source adapter and clean up.
73
+ */
74
+ private unmountCitationSourceAdapter;
75
+ /**
76
+ * Handle attribute field changes.
77
+ */
78
+ private handleAttrChange;
79
+ /**
80
+ * Update node attributes using the command API.
81
+ * Preserves the 'data' attribute if it exists, even though it's not displayed.
82
+ */
83
+ private updateNodeAttribute;
84
+ /**
85
+ * Encode a source string/JSON into the URI-encoded source field format.
86
+ */
87
+ private encodeSourceField;
88
+ }
89
+ declare global {
90
+ interface HTMLElementTagNameMap {
91
+ 'sciflow-selection-editor': SciFlowSelectionEditorElement;
92
+ }
93
+ }
94
+ //# sourceMappingURL=selection-editor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selection-editor.d.ts","sourceRoot":"","sources":["../../src/lib/selection-editor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAa,UAAU,EAAa,MAAM,KAAK,CAAC;AAMvD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAEL,KAAK,2BAA2B,EAEjC,MAAM,8BAA8B,CAAC;AAWtC;;;;;;;;GAQG;AACH,qBACa,6BAA8B,SAAQ,UAAU;IAC3D,oFAAoF;IAEpF,MAAM,EAAE,oBAAoB,GAAG,IAAI,CAAQ;IAE3C,gEAAgE;IAEhE,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,uFAAuF;IAEvF,qBAAqB,EAAE,2BAA2B,GAAG,IAAI,CAAQ;IAGjE,OAAO,CAAC,WAAW,CAAqB;IAExC,OAAO,CAAC,cAAc,CAAqC;IAC3D,OAAO,CAAC,iBAAiB,CAAC,CAAgB;IAC1C,OAAO,CAAC,aAAa,CAAC,CAAgB;IACtC,OAAO,CAAC,UAAU,CAAC,CAAa;IAChC,OAAO,CAAC,kBAAkB,CAA0B;IACpD,OAAO,CAAC,uBAAuB,CAA4B;IAE3D,OAAgB,MAAM,0BAAyC;IAEtD,iBAAiB,IAAI,IAAI;IAYzB,oBAAoB,IAAI,IAAI;IAO5B,OAAO,CAAC,iBAAiB,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;cA+B9D,MAAM;IAyEzB;;OAEG;IACH,OAAO,CAAC,sBAAsB;IA2C9B,OAAO,CAAC,aAAa;IAmBrB,+DAA+D;IAC/D,OAAO,CAAC,qBAAqB;IAc7B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA0DzB,2DAA2D;IAC3D,OAAO,CAAC,gBAAgB;IAIxB;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAwB5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAiB9B;;OAEG;IACH,OAAO,CAAC,0BAA0B;IAuBlC;;OAEG;IACH,OAAO,CAAC,4BAA4B;IASpC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAuBxB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IA2C3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAmB1B;AAGD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,0BAA0B,EAAE,6BAA6B,CAAC;KAC3D;CACF"}