@chordproject/editor 1.1.0 → 1.1.1

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.
package/dist/index.cjs CHANGED
@@ -112,6 +112,35 @@ function chordCompletionSource(context) {
112
112
 
113
113
  // src/autocomplete/snippets.ts
114
114
  var import_autocomplete = require("@codemirror/autocomplete");
115
+ function sectionShortcut(startDirective, endDirective, label, detail) {
116
+ return {
117
+ label,
118
+ type: "directive",
119
+ detail,
120
+ apply: (view, completion, from, to) => {
121
+ const line = view.state.doc.lineAt(from);
122
+ let closingAt = view.state.doc.length;
123
+ for (let lineNumber = line.number + 1; lineNumber <= view.state.doc.lines; lineNumber++) {
124
+ const nextLine = view.state.doc.line(lineNumber);
125
+ if (!nextLine.text.trim() || /^\s*\{\s*[A-Za-z_][\w]*\b/.test(nextLine.text)) {
126
+ closingAt = nextLine.from;
127
+ break;
128
+ }
129
+ }
130
+ const opening = `{${startDirective}}`;
131
+ const previousCharacter = closingAt > 0 ? view.state.doc.sliceString(closingAt - 1, closingAt) : "";
132
+ const closing = `${previousCharacter && previousCharacter !== "\n" ? "\n" : ""}{${endDirective}}
133
+ `;
134
+ view.dispatch({
135
+ changes: [
136
+ { from, to, insert: opening },
137
+ { from: closingAt, insert: closing }
138
+ ],
139
+ selection: { anchor: from + opening.length }
140
+ });
141
+ }
142
+ };
143
+ }
115
144
  var CHORDPRO_SNIPPETS = [
116
145
  (0, import_autocomplete.snippetCompletion)("{album: ${value}}", { label: "album", type: "directive" }),
117
146
  (0, import_autocomplete.snippetCompletion)("{arranger: ${value}}", { label: "arranger", type: "directive" }),
@@ -138,25 +167,25 @@ var CHORDPRO_SNIPPETS = [
138
167
  label: "chorus",
139
168
  type: "directive"
140
169
  }),
141
- (0, import_autocomplete.snippetCompletion)("{start_of_chorus}", { label: "soc", type: "directive", detail: "start_of_chorus" }),
170
+ sectionShortcut("start_of_chorus", "end_of_chorus", "soc", "start_of_chorus"),
142
171
  (0, import_autocomplete.snippetCompletion)("{end_of_chorus}", { label: "eoc", type: "directive", detail: "end_of_chorus" }),
143
172
  (0, import_autocomplete.snippetCompletion)("{start_of_verse: ${Verse} ${1}}\n${lyrics}\n{end_of_verse}", {
144
173
  label: "verse",
145
174
  type: "directive"
146
175
  }),
147
- (0, import_autocomplete.snippetCompletion)("{start_of_verse}", { label: "sov", type: "directive", detail: "start_of_verse" }),
176
+ sectionShortcut("start_of_verse", "end_of_verse", "sov", "start_of_verse"),
148
177
  (0, import_autocomplete.snippetCompletion)("{end_of_verse}", { label: "eov", type: "directive", detail: "end_of_verse" }),
149
178
  (0, import_autocomplete.snippetCompletion)("{start_of_bridge: ${Bridge}}\n${lyrics}\n{end_of_bridge}", {
150
179
  label: "bridge",
151
180
  type: "directive"
152
181
  }),
153
- (0, import_autocomplete.snippetCompletion)("{start_of_bridge}", { label: "sob", type: "directive", detail: "start_of_bridge" }),
182
+ sectionShortcut("start_of_bridge", "end_of_bridge", "sob", "start_of_bridge"),
154
183
  (0, import_autocomplete.snippetCompletion)("{end_of_bridge}", { label: "eob", type: "directive", detail: "end_of_bridge" }),
155
184
  (0, import_autocomplete.snippetCompletion)(
156
185
  "{start_of_tab}\ne|-${1}--------------------------------|\nB|----------------------------------|\nG|----------------------------------|\nD|----------------------------------|\nA|----------------------------------|\nE|----------------------------------|\n{end_of_tab}",
157
186
  { label: "tab", type: "directive" }
158
187
  ),
159
- (0, import_autocomplete.snippetCompletion)("{start_of_tab}", { label: "sot", type: "directive", detail: "start_of_tab" }),
188
+ sectionShortcut("start_of_tab", "end_of_tab", "sot", "start_of_tab"),
160
189
  (0, import_autocomplete.snippetCompletion)("{end_of_tab}", { label: "eot", type: "directive", detail: "end_of_tab" }),
161
190
  (0, import_autocomplete.snippetCompletion)("{define: ${Am} base-fret ${1} frets ${0 0 0 0 0 0} fingers ${0 0 0 0 0 0}}", {
162
191
  label: "define",
@@ -524,6 +553,14 @@ function normalizeChordNotationText(content) {
524
553
  }
525
554
 
526
555
  // src/linting.ts
556
+ var defaultLintLabels = {
557
+ duplicateDirective: (directive, firstLine) => `"${directive}" was already defined on line ${firstLine}; this value replaces it.`,
558
+ parserWarning: (warning) => warning.message,
559
+ sectionMessage: (endDirective) => `This section must be closed with {${endDirective}}.`,
560
+ sectionAction: (endDirective) => `Insert {${endDirective}}`,
561
+ chordNotationMessage: (replacement, reason) => `Use "${replacement}". ${reason}`,
562
+ chordNotationAction: (replacement) => `Replace with ${replacement}`
563
+ };
527
564
  var SINGULAR_DIRECTIVES = {
528
565
  title: "title",
529
566
  t: "title",
@@ -550,161 +587,167 @@ var SECTION_STARTS = {
550
587
  sot: "end_of_tab"
551
588
  };
552
589
  var SECTION_ENDS = /* @__PURE__ */ new Set(["end_of_chorus", "eoc", "end_of_verse", "eov", "end_of_bridge", "eob", "end_of_tab", "eot"]);
553
- var duplicateDirectiveLinter = (0, import_lint2.linter)((view) => {
554
- const diagnostics = [];
555
- const firstSeenAtLine = /* @__PURE__ */ new Map();
556
- for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
557
- const line = view.state.doc.line(lineNumber);
558
- const match = line.text.match(DIRECTIVE_LINE);
559
- if (!match) continue;
560
- const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
561
- if (!canonical) continue;
562
- const firstLine = firstSeenAtLine.get(canonical);
563
- if (firstLine === void 0) {
564
- firstSeenAtLine.set(canonical, lineNumber);
565
- continue;
566
- }
567
- diagnostics.push({
568
- from: line.from,
569
- to: line.from + match[0].length,
570
- severity: "warning",
571
- message: `"${canonical}" ya se defini\xF3 en la l\xEDnea ${firstLine}; este valor la reemplaza.`
572
- });
573
- }
574
- return diagnostics;
575
- });
576
- var parserWarningLinter = (0, import_lint2.linter)((view) => {
577
- const parser = new import_parser.ChordProParser();
578
- parser.parse(view.state.doc.toString());
579
- return parser.warnings.flatMap((warning) => {
580
- if (warning.code === "empty_sheet") {
581
- return [];
582
- }
583
- if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
584
- return [];
590
+ function createLintExtensions(getLabels = () => defaultLintLabels) {
591
+ const duplicateDirectiveLinter = (0, import_lint2.linter)((view) => {
592
+ const diagnostics = [];
593
+ const firstSeenAtLine = /* @__PURE__ */ new Map();
594
+ const labels = getLabels();
595
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
596
+ const line = view.state.doc.line(lineNumber);
597
+ const match = line.text.match(DIRECTIVE_LINE);
598
+ if (!match) continue;
599
+ const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
600
+ if (!canonical) continue;
601
+ const firstLine = firstSeenAtLine.get(canonical);
602
+ if (firstLine === void 0) {
603
+ firstSeenAtLine.set(canonical, lineNumber);
604
+ continue;
605
+ }
606
+ diagnostics.push({
607
+ from: line.from,
608
+ to: line.from + match[0].length,
609
+ severity: "warning",
610
+ message: labels.duplicateDirective(canonical, firstLine)
611
+ });
585
612
  }
586
- const line = view.state.doc.line(warning.lineNumber);
587
- return [{
588
- from: line.from,
589
- to: line.to,
590
- severity: "warning",
591
- message: warning.message
592
- }];
613
+ return diagnostics;
593
614
  });
594
- }, { delay: 0 });
595
- var unclosedSectionLinter = (0, import_lint2.linter)((view) => {
596
- const diagnostics = [];
597
- let openSection = null;
598
- let sectionHasContent = false;
599
- for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
600
- const line = view.state.doc.line(lineNumber);
601
- const match = line.text.match(DIRECTIVE_LINE);
602
- const directive = match?.[1].toLowerCase();
603
- if (directive && SECTION_STARTS[directive]) {
604
- if (openSection) {
605
- openSection.insertionAt = line.from;
606
- openSection.insertionTo = line.from;
607
- diagnostics.push(sectionDiagnostic(openSection));
615
+ const parserWarningLinter = (0, import_lint2.linter)((view) => {
616
+ const parser = new import_parser.ChordProParser();
617
+ parser.parse(view.state.doc.toString());
618
+ const labels = getLabels();
619
+ return parser.warnings.flatMap((warning) => {
620
+ if (warning.code === "empty_sheet") {
621
+ return [];
608
622
  }
609
- openSection = {
610
- endDirective: SECTION_STARTS[directive],
611
- lineFrom: line.from,
612
- lineTo: line.to,
613
- insertionAt: line.from,
614
- insertionTo: line.from
615
- };
616
- sectionHasContent = false;
617
- continue;
618
- }
619
- if (directive && SECTION_ENDS.has(directive)) {
620
- openSection = null;
621
- sectionHasContent = false;
622
- continue;
623
- }
624
- if (openSection && directive) {
625
- openSection.insertionAt = line.from;
626
- openSection.insertionTo = line.from;
627
- diagnostics.push(sectionDiagnostic(openSection));
628
- openSection = null;
629
- sectionHasContent = false;
630
- continue;
631
- }
632
- if (openSection && !line.text.trim()) {
633
- const nextDirective = nextNonBlankDirective(view, lineNumber);
634
- if (sectionHasContent) {
623
+ if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
624
+ return [];
625
+ }
626
+ const line = view.state.doc.line(warning.lineNumber);
627
+ return [{
628
+ from: line.from,
629
+ to: line.to,
630
+ severity: "warning",
631
+ message: labels.parserWarning(warning)
632
+ }];
633
+ });
634
+ }, { delay: 0 });
635
+ const unclosedSectionLinter = (0, import_lint2.linter)((view) => {
636
+ const diagnostics = [];
637
+ let openSection = null;
638
+ let sectionHasContent = false;
639
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
640
+ const line = view.state.doc.line(lineNumber);
641
+ const match = line.text.match(DIRECTIVE_LINE);
642
+ const directive = match?.[1].toLowerCase();
643
+ if (directive && SECTION_STARTS[directive]) {
644
+ if (openSection) {
645
+ openSection.insertionAt = line.from;
646
+ openSection.insertionTo = line.from;
647
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
648
+ }
649
+ openSection = {
650
+ endDirective: SECTION_STARTS[directive],
651
+ lineFrom: line.from,
652
+ lineTo: line.to,
653
+ insertionAt: line.from,
654
+ insertionTo: line.from
655
+ };
656
+ sectionHasContent = false;
657
+ continue;
658
+ }
659
+ if (directive && SECTION_ENDS.has(directive)) {
660
+ openSection = null;
661
+ sectionHasContent = false;
662
+ continue;
663
+ }
664
+ if (openSection && directive) {
635
665
  openSection.insertionAt = line.from;
636
- openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
637
- diagnostics.push(sectionDiagnostic(openSection));
666
+ openSection.insertionTo = line.from;
667
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
638
668
  openSection = null;
639
669
  sectionHasContent = false;
670
+ continue;
640
671
  }
641
- continue;
642
- }
643
- if (openSection && lineNumber === view.state.doc.lines) {
644
- openSection.insertionAt = view.state.doc.length;
645
- openSection.insertionTo = view.state.doc.length;
646
- }
647
- if (openSection) {
648
- sectionHasContent = true;
649
- }
650
- }
651
- function nextNonBlankDirective(view2, lineNumber) {
652
- const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
653
- return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
654
- }
655
- function nextNonBlankDirectiveLine(view2, lineNumber) {
656
- for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
657
- const nextLine = view2.state.doc.line(nextLineNumber);
658
- if (!nextLine.text.trim()) {
672
+ if (openSection && !line.text.trim()) {
673
+ const nextDirective = nextNonBlankDirective(view, lineNumber);
674
+ if (sectionHasContent) {
675
+ openSection.insertionAt = line.from;
676
+ openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
677
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
678
+ openSection = null;
679
+ sectionHasContent = false;
680
+ }
659
681
  continue;
660
682
  }
661
- if (nextLine.text.match(DIRECTIVE_LINE)) {
662
- return nextLine.from;
683
+ if (openSection && lineNumber === view.state.doc.lines) {
684
+ openSection.insertionAt = view.state.doc.length;
685
+ openSection.insertionTo = view.state.doc.length;
686
+ }
687
+ if (openSection) {
688
+ sectionHasContent = true;
663
689
  }
664
- return view2.state.doc.length;
665
690
  }
666
- return view2.state.doc.length;
667
- }
668
- if (openSection) {
669
- diagnostics.push(sectionDiagnostic(openSection));
670
- }
671
- return diagnostics;
672
- }, { delay: 0 });
673
- function sectionDiagnostic(section) {
674
- return {
675
- from: section.lineFrom,
676
- to: section.lineTo,
677
- severity: "warning",
678
- message: `Falta cerrar esta secci\xF3n con {${section.endDirective}}.`,
679
- actions: [{
680
- name: `Insertar {${section.endDirective}}`,
681
- apply: (view, from, to) => {
682
- const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
683
- const needsLeadingNewline = previousCharacter !== "\n";
684
- const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
685
- view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
691
+ function nextNonBlankDirective(view2, lineNumber) {
692
+ const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
693
+ return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
694
+ }
695
+ function nextNonBlankDirectiveLine(view2, lineNumber) {
696
+ for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
697
+ const nextLine = view2.state.doc.line(nextLineNumber);
698
+ if (!nextLine.text.trim()) {
699
+ continue;
700
+ }
701
+ if (nextLine.text.match(DIRECTIVE_LINE)) {
702
+ return nextLine.from;
703
+ }
704
+ return view2.state.doc.length;
686
705
  }
687
- }]
688
- };
689
- }
690
- var chordNotationLinter = (0, import_lint2.linter)((view) => {
691
- const diagnostics = [];
692
- for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
693
- diagnostics.push({
694
- from: replacement.from,
695
- to: replacement.to,
696
- severity: "info",
697
- message: `Usa "${replacement.replacement}". ${replacement.reason}`,
706
+ return view2.state.doc.length;
707
+ }
708
+ if (openSection) {
709
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
710
+ }
711
+ return diagnostics;
712
+ }, { delay: 0 });
713
+ function sectionDiagnostic(section, labels) {
714
+ return {
715
+ from: section.lineFrom,
716
+ to: section.lineTo,
717
+ severity: "warning",
718
+ message: labels.sectionMessage(section.endDirective),
698
719
  actions: [{
699
- name: `Reemplazar por ${replacement.replacement}`,
700
- apply: (targetView, actionFrom, actionTo) => {
701
- targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
720
+ name: labels.sectionAction(section.endDirective),
721
+ apply: (view, from, to) => {
722
+ const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
723
+ const needsLeadingNewline = previousCharacter !== "\n";
724
+ const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
725
+ view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
702
726
  }
703
727
  }]
704
- });
728
+ };
705
729
  }
706
- return diagnostics;
707
- });
730
+ const chordNotationLinter = (0, import_lint2.linter)((view) => {
731
+ const diagnostics = [];
732
+ const labels = getLabels();
733
+ for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
734
+ diagnostics.push({
735
+ from: replacement.from,
736
+ to: replacement.to,
737
+ severity: "info",
738
+ message: labels.chordNotationMessage(replacement.replacement, replacement.reason),
739
+ actions: [{
740
+ name: labels.chordNotationAction(replacement.replacement),
741
+ apply: (targetView, actionFrom, actionTo) => {
742
+ targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
743
+ }
744
+ }]
745
+ });
746
+ }
747
+ return diagnostics;
748
+ });
749
+ return [duplicateDirectiveLinter, parserWarningLinter, unclosedSectionLinter, chordNotationLinter];
750
+ }
708
751
 
709
752
  // src/themes/base.ts
710
753
  var import_view2 = require("@codemirror/view");
@@ -1303,16 +1346,14 @@ function createChordProEditor(options) {
1303
1346
  (update.view.hasFocus ? options.onFocus : options.onBlur)?.();
1304
1347
  }
1305
1348
  });
1349
+ const lintExtensions = createLintExtensions(options.lintLabels ?? (() => defaultLintLabels));
1306
1350
  const state = import_state2.EditorState.create({
1307
1351
  doc: options.doc ?? "",
1308
1352
  extensions: [
1309
1353
  basicSetup(),
1310
1354
  chordPro(),
1311
1355
  chordProFolding,
1312
- duplicateDirectiveLinter,
1313
- parserWarningLinter,
1314
- unclosedSectionLinter,
1315
- chordNotationLinter,
1356
+ ...lintExtensions,
1316
1357
  chordProAutocomplete(),
1317
1358
  themeCompartment.of(themeExtension(options.theme ?? "light")),
1318
1359
  (0, import_search3.search)({ top: true, createPanel: (view2) => createSearchPanel(view2, options.searchLabels?.()) }),
@@ -1353,6 +1394,7 @@ function createChordProEditor(options) {
1353
1394
  }
1354
1395
  return normalization.suggestions;
1355
1396
  },
1397
+ refreshLint: () => view.dispatch({}),
1356
1398
  focus: () => view.focus(),
1357
1399
  destroy: () => view.destroy()
1358
1400
  };