@chordproject/editor 1.0.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.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import { Compartment, EditorState as EditorState2 } from "@codemirror/state";
3
3
  import { EditorView as EditorView5 } from "@codemirror/view";
4
+ import { openSearchPanel, search } from "@codemirror/search";
4
5
 
5
6
  // src/autocomplete/index.ts
6
7
  import {
@@ -88,6 +89,35 @@ function chordCompletionSource(context) {
88
89
 
89
90
  // src/autocomplete/snippets.ts
90
91
  import { snippetCompletion } from "@codemirror/autocomplete";
92
+ function sectionShortcut(startDirective, endDirective, label, detail) {
93
+ return {
94
+ label,
95
+ type: "directive",
96
+ detail,
97
+ apply: (view, completion, from, to) => {
98
+ const line = view.state.doc.lineAt(from);
99
+ let closingAt = view.state.doc.length;
100
+ for (let lineNumber = line.number + 1; lineNumber <= view.state.doc.lines; lineNumber++) {
101
+ const nextLine = view.state.doc.line(lineNumber);
102
+ if (!nextLine.text.trim() || /^\s*\{\s*[A-Za-z_][\w]*\b/.test(nextLine.text)) {
103
+ closingAt = nextLine.from;
104
+ break;
105
+ }
106
+ }
107
+ const opening = `{${startDirective}}`;
108
+ const previousCharacter = closingAt > 0 ? view.state.doc.sliceString(closingAt - 1, closingAt) : "";
109
+ const closing = `${previousCharacter && previousCharacter !== "\n" ? "\n" : ""}{${endDirective}}
110
+ `;
111
+ view.dispatch({
112
+ changes: [
113
+ { from, to, insert: opening },
114
+ { from: closingAt, insert: closing }
115
+ ],
116
+ selection: { anchor: from + opening.length }
117
+ });
118
+ }
119
+ };
120
+ }
91
121
  var CHORDPRO_SNIPPETS = [
92
122
  snippetCompletion("{album: ${value}}", { label: "album", type: "directive" }),
93
123
  snippetCompletion("{arranger: ${value}}", { label: "arranger", type: "directive" }),
@@ -114,25 +144,25 @@ var CHORDPRO_SNIPPETS = [
114
144
  label: "chorus",
115
145
  type: "directive"
116
146
  }),
117
- snippetCompletion("{start_of_chorus}", { label: "soc", type: "directive", detail: "start_of_chorus" }),
147
+ sectionShortcut("start_of_chorus", "end_of_chorus", "soc", "start_of_chorus"),
118
148
  snippetCompletion("{end_of_chorus}", { label: "eoc", type: "directive", detail: "end_of_chorus" }),
119
149
  snippetCompletion("{start_of_verse: ${Verse} ${1}}\n${lyrics}\n{end_of_verse}", {
120
150
  label: "verse",
121
151
  type: "directive"
122
152
  }),
123
- snippetCompletion("{start_of_verse}", { label: "sov", type: "directive", detail: "start_of_verse" }),
153
+ sectionShortcut("start_of_verse", "end_of_verse", "sov", "start_of_verse"),
124
154
  snippetCompletion("{end_of_verse}", { label: "eov", type: "directive", detail: "end_of_verse" }),
125
155
  snippetCompletion("{start_of_bridge: ${Bridge}}\n${lyrics}\n{end_of_bridge}", {
126
156
  label: "bridge",
127
157
  type: "directive"
128
158
  }),
129
- snippetCompletion("{start_of_bridge}", { label: "sob", type: "directive", detail: "start_of_bridge" }),
159
+ sectionShortcut("start_of_bridge", "end_of_bridge", "sob", "start_of_bridge"),
130
160
  snippetCompletion("{end_of_bridge}", { label: "eob", type: "directive", detail: "end_of_bridge" }),
131
161
  snippetCompletion(
132
162
  "{start_of_tab}\ne|-${1}--------------------------------|\nB|----------------------------------|\nG|----------------------------------|\nD|----------------------------------|\nA|----------------------------------|\nE|----------------------------------|\n{end_of_tab}",
133
163
  { label: "tab", type: "directive" }
134
164
  ),
135
- snippetCompletion("{start_of_tab}", { label: "sot", type: "directive", detail: "start_of_tab" }),
165
+ sectionShortcut("start_of_tab", "end_of_tab", "sot", "start_of_tab"),
136
166
  snippetCompletion("{end_of_tab}", { label: "eot", type: "directive", detail: "end_of_tab" }),
137
167
  snippetCompletion("{define: ${Am} base-fret ${1} frets ${0 0 0 0 0 0} fingers ${0 0 0 0 0 0}}", {
138
168
  label: "define",
@@ -152,10 +182,15 @@ var CHORDPRO_SNIPPETS = [
152
182
 
153
183
  // src/autocomplete/index.ts
154
184
  var completeSnippets = completeFromList(CHORDPRO_SNIPPETS);
185
+ var QUICK_SNIPPET_PREFIXES = new Set(
186
+ CHORDPRO_SNIPPETS.map((snippet) => snippet.label).filter((label) => label.length <= 3 && /^[a-z]+$/i.test(label))
187
+ );
155
188
  function chordProCompletionSource(context) {
156
189
  const chordCompletions = chordCompletionSource(context);
157
190
  if (chordCompletions) return chordCompletions;
158
- return context.explicit ? completeSnippets(context) : null;
191
+ const word = context.matchBefore(/[a-z]+$/i);
192
+ const isQuickSnippet = word && QUICK_SNIPPET_PREFIXES.has(word.text.toLowerCase());
193
+ return context.explicit || isQuickSnippet ? completeSnippets(context) : null;
159
194
  }
160
195
  function chordProAutocomplete() {
161
196
  return autocompletion({ override: [chordProCompletionSource], activateOnTyping: true });
@@ -188,6 +223,7 @@ import {
188
223
  import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
189
224
  import { bracketMatching, foldGutter, foldKeymap } from "@codemirror/language";
190
225
  import { lintGutter, lintKeymap } from "@codemirror/lint";
226
+ import { searchKeymap } from "@codemirror/search";
191
227
  import {
192
228
  acceptCompletion,
193
229
  closeBrackets,
@@ -235,6 +271,7 @@ function basicSetup() {
235
271
  ...historyKeymap,
236
272
  ...foldKeymap,
237
273
  ...lintKeymap,
274
+ ...searchKeymap,
238
275
  // Override completionKeymap's Ctrl-Space/Option-`/Option-i (same keys, listed first so
239
276
  // they take precedence) to be chord-aware instead of plain startCompletion.
240
277
  { key: "Ctrl-Space", run: chordAwareCompletion },
@@ -506,6 +543,14 @@ function normalizeChordNotationText(content) {
506
543
  }
507
544
 
508
545
  // src/linting.ts
546
+ var defaultLintLabels = {
547
+ duplicateDirective: (directive, firstLine) => `"${directive}" was already defined on line ${firstLine}; this value replaces it.`,
548
+ parserWarning: (warning) => warning.message,
549
+ sectionMessage: (endDirective) => `This section must be closed with {${endDirective}}.`,
550
+ sectionAction: (endDirective) => `Insert {${endDirective}}`,
551
+ chordNotationMessage: (replacement, reason) => `Use "${replacement}". ${reason}`,
552
+ chordNotationAction: (replacement) => `Replace with ${replacement}`
553
+ };
509
554
  var SINGULAR_DIRECTIVES = {
510
555
  title: "title",
511
556
  t: "title",
@@ -520,63 +565,179 @@ var SINGULAR_DIRECTIVES = {
520
565
  time: "time"
521
566
  };
522
567
  var DIRECTIVE_LINE = /^\s*\{\s*([A-Za-z_][\w]*)\b/;
523
- var duplicateDirectiveLinter = linter((view) => {
524
- const diagnostics = [];
525
- const firstSeenAtLine = /* @__PURE__ */ new Map();
526
- for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
527
- const line = view.state.doc.line(lineNumber);
528
- const match = line.text.match(DIRECTIVE_LINE);
529
- if (!match) continue;
530
- const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
531
- if (!canonical) continue;
532
- const firstLine = firstSeenAtLine.get(canonical);
533
- if (firstLine === void 0) {
534
- firstSeenAtLine.set(canonical, lineNumber);
535
- continue;
568
+ var SECTION_STARTS = {
569
+ start_of_chorus: "end_of_chorus",
570
+ soc: "end_of_chorus",
571
+ chorus: "end_of_chorus",
572
+ start_of_verse: "end_of_verse",
573
+ sov: "end_of_verse",
574
+ start_of_bridge: "end_of_bridge",
575
+ sob: "end_of_bridge",
576
+ start_of_tab: "end_of_tab",
577
+ sot: "end_of_tab"
578
+ };
579
+ var SECTION_ENDS = /* @__PURE__ */ new Set(["end_of_chorus", "eoc", "end_of_verse", "eov", "end_of_bridge", "eob", "end_of_tab", "eot"]);
580
+ function createLintExtensions(getLabels = () => defaultLintLabels) {
581
+ const duplicateDirectiveLinter = linter((view) => {
582
+ const diagnostics = [];
583
+ const firstSeenAtLine = /* @__PURE__ */ new Map();
584
+ const labels = getLabels();
585
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
586
+ const line = view.state.doc.line(lineNumber);
587
+ const match = line.text.match(DIRECTIVE_LINE);
588
+ if (!match) continue;
589
+ const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
590
+ if (!canonical) continue;
591
+ const firstLine = firstSeenAtLine.get(canonical);
592
+ if (firstLine === void 0) {
593
+ firstSeenAtLine.set(canonical, lineNumber);
594
+ continue;
595
+ }
596
+ diagnostics.push({
597
+ from: line.from,
598
+ to: line.from + match[0].length,
599
+ severity: "warning",
600
+ message: labels.duplicateDirective(canonical, firstLine)
601
+ });
536
602
  }
537
- diagnostics.push({
538
- from: line.from,
539
- to: line.from + match[0].length,
540
- severity: "warning",
541
- message: `"${canonical}" ya se defini\xF3 en la l\xEDnea ${firstLine}; este valor la reemplaza.`
603
+ return diagnostics;
604
+ });
605
+ const parserWarningLinter = linter((view) => {
606
+ const parser = new ChordProParser();
607
+ parser.parse(view.state.doc.toString());
608
+ const labels = getLabels();
609
+ return parser.warnings.flatMap((warning) => {
610
+ if (warning.code === "empty_sheet") {
611
+ return [];
612
+ }
613
+ if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
614
+ return [];
615
+ }
616
+ const line = view.state.doc.line(warning.lineNumber);
617
+ return [{
618
+ from: line.from,
619
+ to: line.to,
620
+ severity: "warning",
621
+ message: labels.parserWarning(warning)
622
+ }];
542
623
  });
543
- }
544
- return diagnostics;
545
- });
546
- var parserWarningLinter = linter((view) => {
547
- const parser = new ChordProParser();
548
- parser.parse(view.state.doc.toString());
549
- return parser.warnings.flatMap((warning) => {
550
- if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
551
- return [];
624
+ }, { delay: 0 });
625
+ const unclosedSectionLinter = linter((view) => {
626
+ const diagnostics = [];
627
+ let openSection = null;
628
+ let sectionHasContent = false;
629
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
630
+ const line = view.state.doc.line(lineNumber);
631
+ const match = line.text.match(DIRECTIVE_LINE);
632
+ const directive = match?.[1].toLowerCase();
633
+ if (directive && SECTION_STARTS[directive]) {
634
+ if (openSection) {
635
+ openSection.insertionAt = line.from;
636
+ openSection.insertionTo = line.from;
637
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
638
+ }
639
+ openSection = {
640
+ endDirective: SECTION_STARTS[directive],
641
+ lineFrom: line.from,
642
+ lineTo: line.to,
643
+ insertionAt: line.from,
644
+ insertionTo: line.from
645
+ };
646
+ sectionHasContent = false;
647
+ continue;
648
+ }
649
+ if (directive && SECTION_ENDS.has(directive)) {
650
+ openSection = null;
651
+ sectionHasContent = false;
652
+ continue;
653
+ }
654
+ if (openSection && directive) {
655
+ openSection.insertionAt = line.from;
656
+ openSection.insertionTo = line.from;
657
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
658
+ openSection = null;
659
+ sectionHasContent = false;
660
+ continue;
661
+ }
662
+ if (openSection && !line.text.trim()) {
663
+ const nextDirective = nextNonBlankDirective(view, lineNumber);
664
+ if (sectionHasContent) {
665
+ openSection.insertionAt = line.from;
666
+ openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
667
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
668
+ openSection = null;
669
+ sectionHasContent = false;
670
+ }
671
+ continue;
672
+ }
673
+ if (openSection && lineNumber === view.state.doc.lines) {
674
+ openSection.insertionAt = view.state.doc.length;
675
+ openSection.insertionTo = view.state.doc.length;
676
+ }
677
+ if (openSection) {
678
+ sectionHasContent = true;
679
+ }
680
+ }
681
+ function nextNonBlankDirective(view2, lineNumber) {
682
+ const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
683
+ return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
552
684
  }
553
- const line = view.state.doc.line(warning.lineNumber);
554
- return [{
555
- from: line.from,
556
- to: line.to,
685
+ function nextNonBlankDirectiveLine(view2, lineNumber) {
686
+ for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
687
+ const nextLine = view2.state.doc.line(nextLineNumber);
688
+ if (!nextLine.text.trim()) {
689
+ continue;
690
+ }
691
+ if (nextLine.text.match(DIRECTIVE_LINE)) {
692
+ return nextLine.from;
693
+ }
694
+ return view2.state.doc.length;
695
+ }
696
+ return view2.state.doc.length;
697
+ }
698
+ if (openSection) {
699
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
700
+ }
701
+ return diagnostics;
702
+ }, { delay: 0 });
703
+ function sectionDiagnostic(section, labels) {
704
+ return {
705
+ from: section.lineFrom,
706
+ to: section.lineTo,
557
707
  severity: "warning",
558
- message: warning.message
559
- }];
560
- });
561
- });
562
- var chordNotationLinter = linter((view) => {
563
- const diagnostics = [];
564
- for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
565
- diagnostics.push({
566
- from: replacement.from,
567
- to: replacement.to,
568
- severity: "info",
569
- message: `Usa "${replacement.replacement}". ${replacement.reason}`,
708
+ message: labels.sectionMessage(section.endDirective),
570
709
  actions: [{
571
- name: `Reemplazar por ${replacement.replacement}`,
572
- apply: (targetView, actionFrom, actionTo) => {
573
- targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
710
+ name: labels.sectionAction(section.endDirective),
711
+ apply: (view, from, to) => {
712
+ const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
713
+ const needsLeadingNewline = previousCharacter !== "\n";
714
+ const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
715
+ view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
574
716
  }
575
717
  }]
576
- });
718
+ };
577
719
  }
578
- return diagnostics;
579
- });
720
+ const chordNotationLinter = linter((view) => {
721
+ const diagnostics = [];
722
+ const labels = getLabels();
723
+ for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
724
+ diagnostics.push({
725
+ from: replacement.from,
726
+ to: replacement.to,
727
+ severity: "info",
728
+ message: labels.chordNotationMessage(replacement.replacement, replacement.reason),
729
+ actions: [{
730
+ name: labels.chordNotationAction(replacement.replacement),
731
+ apply: (targetView, actionFrom, actionTo) => {
732
+ targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
733
+ }
734
+ }]
735
+ });
736
+ }
737
+ return diagnostics;
738
+ });
739
+ return [duplicateDirectiveLinter, parserWarningLinter, unclosedSectionLinter, chordNotationLinter];
740
+ }
580
741
 
581
742
  // src/themes/base.ts
582
743
  import { EditorView as EditorView2 } from "@codemirror/view";
@@ -588,14 +749,182 @@ var baseTheme = EditorView2.baseTheme({
588
749
  flex: "1",
589
750
  overflow: "auto"
590
751
  },
752
+ ".cm-selectionLayer": {
753
+ zIndex: "-1 !important"
754
+ },
755
+ ".chord-search-panel": {
756
+ position: "absolute",
757
+ top: "0.4rem",
758
+ right: "0.5rem",
759
+ zIndex: "20",
760
+ pointerEvents: "auto",
761
+ display: "flex",
762
+ width: "min(100% - 1rem, 32rem)",
763
+ margin: "0",
764
+ border: "1px solid var(--color-neutral-700, #3f3f46)",
765
+ borderRadius: "0.45rem",
766
+ boxShadow: "0 0.5rem 1.25rem color-mix(in oklch, #000 28%, transparent)",
767
+ flexWrap: "wrap",
768
+ alignItems: "center",
769
+ gap: "0.3rem",
770
+ padding: "0.35rem 0.45rem",
771
+ borderBottom: "1px solid var(--color-neutral-200, #e4e4e7)",
772
+ backgroundColor: "var(--color-white, #ffffff)",
773
+ fontSize: "0.875rem"
774
+ },
775
+ ".chord-search-controls": {
776
+ display: "flex",
777
+ flex: "1 1 100%",
778
+ flexWrap: "nowrap",
779
+ alignItems: "center",
780
+ gap: "0.25rem"
781
+ },
782
+ ".chord-search-navigation": {
783
+ display: "flex",
784
+ flex: "0 0 auto",
785
+ alignItems: "center",
786
+ gap: "0.2rem"
787
+ },
788
+ ".chord-search-fields": {
789
+ display: "flex",
790
+ flex: "1 1 auto",
791
+ minWidth: "0",
792
+ flexDirection: "column",
793
+ gap: "0.25rem"
794
+ },
795
+ ".chord-search-replace-row": {
796
+ display: "flex",
797
+ flex: "1 1 100%",
798
+ flexWrap: "nowrap",
799
+ alignItems: "center",
800
+ gap: "0.25rem"
801
+ },
802
+ ".chord-search-replace-row[hidden]": {
803
+ display: "none"
804
+ },
805
+ ".chord-search-field": {
806
+ flex: "1 1 12rem",
807
+ border: "1px solid var(--color-neutral-300, #d4d4d8)",
808
+ borderRadius: "0.35rem",
809
+ backgroundColor: "var(--color-white, #ffffff)"
810
+ },
811
+ ".chord-search-match-count": {
812
+ flex: "0 0 auto",
813
+ minWidth: "3.5rem",
814
+ color: "var(--color-neutral-500, #71717a)",
815
+ fontSize: "0.75rem",
816
+ textAlign: "center",
817
+ whiteSpace: "nowrap"
818
+ },
819
+ ".chord-search-field input": {
820
+ boxSizing: "border-box",
821
+ width: "100%",
822
+ minHeight: "1.9rem",
823
+ padding: "0.3rem 0.5rem",
824
+ border: "none",
825
+ outline: "none",
826
+ backgroundColor: "transparent",
827
+ color: "inherit",
828
+ font: "inherit"
829
+ },
830
+ ".chord-search-input-with-options": {
831
+ display: "flex",
832
+ alignItems: "center",
833
+ gap: "0.1rem",
834
+ paddingRight: "0.15rem"
835
+ },
836
+ ".chord-search-input-with-options input": {
837
+ minWidth: "0",
838
+ border: "none",
839
+ outline: "none",
840
+ backgroundColor: "transparent",
841
+ paddingRight: "0.2rem"
842
+ },
843
+ ".chord-search-input-with-options .chord-search-option": {
844
+ minWidth: "1.6rem",
845
+ minHeight: "1.6rem",
846
+ zIndex: "2",
847
+ padding: "0.1rem",
848
+ border: "none",
849
+ backgroundColor: "transparent",
850
+ fontSize: "0.7rem"
851
+ },
852
+ ".chord-search-button": {
853
+ minHeight: "1.9rem",
854
+ minWidth: "1.9rem",
855
+ padding: "0.2rem 0.45rem",
856
+ border: "1px solid var(--color-neutral-300, #d4d4d8)",
857
+ borderRadius: "0.35rem",
858
+ backgroundColor: "var(--color-neutral-50, #fafafa)",
859
+ color: "inherit",
860
+ font: "inherit",
861
+ cursor: "pointer"
862
+ },
863
+ ".chord-search-button:hover": {
864
+ backgroundColor: "var(--color-neutral-100, #f4f4f5)"
865
+ },
866
+ ".chord-search-toggle-replace, .chord-search-previous, .chord-search-next, .chord-search-close": {
867
+ border: "none",
868
+ backgroundColor: "transparent"
869
+ },
870
+ ".chord-search-toggle-replace:hover, .chord-search-previous:hover, .chord-search-next:hover, .chord-search-close:hover": {
871
+ backgroundColor: "var(--color-neutral-100, #f4f4f5)"
872
+ },
873
+ ".chord-search-close": {
874
+ position: "static",
875
+ fontSize: "1.15rem",
876
+ lineHeight: "1"
877
+ },
878
+ ".chord-search-options": {
879
+ display: "flex",
880
+ flexWrap: "wrap",
881
+ gap: "0.8rem",
882
+ color: "var(--color-neutral-600, #52525b)"
883
+ },
884
+ ".chord-search-controls .chord-search-field": {
885
+ minWidth: "0",
886
+ flex: "1 1 auto"
887
+ },
888
+ ".chord-search-replace-row .chord-search-field": {
889
+ minWidth: "0",
890
+ flex: "1 1 auto"
891
+ },
892
+ ".chord-search-option": {
893
+ display: "inline-flex",
894
+ alignItems: "center",
895
+ gap: "0.35rem",
896
+ cursor: "pointer"
897
+ },
898
+ ".chord-search-toggle": {
899
+ minWidth: "2.25rem",
900
+ justifyContent: "center",
901
+ fontWeight: "600"
902
+ },
903
+ ".chord-search-toggle input": {
904
+ display: "none"
905
+ },
906
+ ".chord-search-toggle.is-active": {
907
+ borderColor: "var(--color-primary-500, #3b82f6)",
908
+ backgroundColor: "var(--color-primary-100, #dbeafe)",
909
+ color: "var(--color-primary-700, #1d4ed8)"
910
+ },
591
911
  ".cm-lintRange-info": {
592
- backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23dc2626" fill="none" stroke-width="1"/></svg>") !important'
912
+ backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23dc2626" fill="none" stroke-width="1"/></svg>") !important',
913
+ backgroundSize: "6px 3px",
914
+ backgroundPosition: "left bottom",
915
+ backgroundRepeat: "repeat-x"
593
916
  },
594
917
  ".cm-lintRange-warning": {
595
- backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23d97706" fill="none" stroke-width="1"/></svg>") !important'
918
+ backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23d97706" fill="none" stroke-width="1"/></svg>") !important',
919
+ backgroundSize: "6px 3px",
920
+ backgroundPosition: "left bottom",
921
+ backgroundRepeat: "repeat-x"
596
922
  },
597
923
  ".cm-lintRange-error": {
598
- backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23dc2626" fill="none" stroke-width="1"/></svg>") !important'
924
+ backgroundImage: 'url("data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3"><path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="%23dc2626" fill="none" stroke-width="1"/></svg>") !important',
925
+ backgroundSize: "6px 3px",
926
+ backgroundPosition: "left bottom",
927
+ backgroundRepeat: "repeat-x"
599
928
  }
600
929
  });
601
930
 
@@ -610,23 +939,25 @@ var editorTheme = EditorView3.theme(
610
939
  color: "var(--color-neutral-100, #f4f4f5)"
611
940
  },
612
941
  ".cm-content": {
942
+ textDecoration: "none",
613
943
  caretColor: "var(--color-neutral-100, #f4f4f5)"
614
944
  },
615
945
  ".cm-cursor, .cm-dropCursor": {
616
946
  borderLeftColor: "var(--color-neutral-100, #f4f4f5)"
617
947
  },
618
948
  "&.cm-focused .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection": {
619
- backgroundColor: "color-mix(in oklch, var(--color-primary-400, #60a5fa) 88%, transparent) !important"
949
+ backgroundColor: "var(--color-primary-700, #1d4ed8) !important",
950
+ color: "inherit !important"
620
951
  },
621
952
  "&.cm-focused .cm-content:focus::selection, &.cm-focused .cm-content .cm-line::selection": {
622
- backgroundColor: "color-mix(in oklch, var(--color-primary-400, #60a5fa) 54%, transparent) !important",
953
+ backgroundColor: "var(--color-primary-700, #1d4ed8) !important",
623
954
  color: "inherit !important"
624
955
  },
625
956
  ".cm-selectionLayer .cm-selectionBackground": {
626
- boxShadow: "inset 0 -2px 0 var(--color-primary-200, #bfdbfe)"
957
+ boxShadow: "none"
627
958
  },
628
959
  ".cm-activeLine": {
629
- backgroundColor: "var(--color-neutral-800, #27272a)"
960
+ backgroundColor: "transparent"
630
961
  },
631
962
  ".cm-gutters": {
632
963
  backgroundColor: "var(--color-neutral-900, #18181b)",
@@ -636,11 +967,53 @@ var editorTheme = EditorView3.theme(
636
967
  ".cm-activeLineGutter": {
637
968
  backgroundColor: "var(--color-neutral-800, #27272a)"
638
969
  },
970
+ ".chord-search-panel": {
971
+ borderBottomColor: "var(--color-neutral-700, #3f3f46)",
972
+ backgroundColor: "var(--color-neutral-900, #18181b)",
973
+ borderColor: "var(--color-neutral-700, #3f3f46)"
974
+ },
975
+ ".chord-search-field input": {
976
+ backgroundColor: "var(--color-neutral-800, #27272a)",
977
+ color: "var(--color-neutral-100, #f4f4f5)"
978
+ },
979
+ ".chord-search-field": {
980
+ borderColor: "var(--color-neutral-700, #3f3f46)",
981
+ backgroundColor: "var(--color-neutral-800, #27272a)"
982
+ },
983
+ ".chord-search-input-with-options input": {
984
+ backgroundColor: "transparent"
985
+ },
986
+ ".chord-search-button": {
987
+ borderColor: "var(--color-neutral-700, #3f3f46)",
988
+ backgroundColor: "var(--color-neutral-800, #27272a)",
989
+ color: "var(--color-neutral-100, #f4f4f5)"
990
+ },
991
+ ".chord-search-button:hover": {
992
+ backgroundColor: "var(--color-neutral-700, #3f3f46)"
993
+ },
994
+ ".chord-search-toggle-replace:hover, .chord-search-previous:hover, .chord-search-next:hover, .chord-search-close:hover": {
995
+ backgroundColor: "var(--color-neutral-800, #27272a)"
996
+ },
997
+ ".chord-search-options": {
998
+ color: "var(--color-neutral-300, #d4d4d8)"
999
+ },
1000
+ ".chord-search-toggle.is-active": {
1001
+ borderColor: "var(--color-primary-400, #60a5fa)",
1002
+ backgroundColor: "var(--color-primary-950, #172554)",
1003
+ color: "var(--color-primary-200, #bfdbfe)"
1004
+ },
639
1005
  ".cm-completionIcon-chord-used + .cm-completionLabel": {
640
1006
  color: "var(--color-primary-300, #93c5fd)",
641
1007
  fontWeight: "600"
642
1008
  },
643
- '.cm-tooltip-autocomplete > ul > li[aria-selected="true"] .cm-completionIcon-chord-used + .cm-completionLabel': {
1009
+ ".cm-tooltip-autocomplete ul li[aria-selected] .cm-completionIcon-chord-used + .cm-completionLabel": {
1010
+ color: "var(--color-white, #ffffff)"
1011
+ },
1012
+ ".cm-tooltip-autocomplete ul li[aria-selected]": {
1013
+ backgroundColor: "var(--color-primary-700, #1d4ed8)",
1014
+ color: "var(--color-white, #ffffff)"
1015
+ },
1016
+ ".cm-tooltip-autocomplete ul li[aria-selected] .cm-completionLabel, .cm-tooltip-autocomplete ul li[aria-selected] .cm-completionDetail, .cm-tooltip-autocomplete ul li[aria-selected] .cm-completionMatchedText": {
644
1017
  color: "var(--color-white, #ffffff)"
645
1018
  },
646
1019
  ".cm-lintRange-info": {
@@ -686,23 +1059,25 @@ var editorTheme2 = EditorView4.theme(
686
1059
  color: "var(--color-neutral-950, #09090b)"
687
1060
  },
688
1061
  ".cm-content": {
1062
+ textDecoration: "none",
689
1063
  caretColor: "var(--color-neutral-950, #09090b)"
690
1064
  },
691
1065
  ".cm-cursor, .cm-dropCursor": {
692
1066
  borderLeftColor: "var(--color-neutral-950, #09090b)"
693
1067
  },
694
1068
  "&.cm-focused .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection": {
695
- backgroundColor: "color-mix(in oklch, var(--color-primary-500, #3b82f6) 78%, transparent) !important"
1069
+ backgroundColor: "var(--color-primary-200, #bfdbfe) !important",
1070
+ color: "inherit !important"
696
1071
  },
697
1072
  "&.cm-focused .cm-content:focus::selection, &.cm-focused .cm-content .cm-line::selection": {
698
- backgroundColor: "color-mix(in oklch, var(--color-primary-500, #3b82f6) 52%, transparent) !important",
1073
+ backgroundColor: "var(--color-primary-200, #bfdbfe) !important",
699
1074
  color: "inherit !important"
700
1075
  },
701
1076
  ".cm-selectionLayer .cm-selectionBackground": {
702
- boxShadow: "inset 0 -2px 0 var(--color-primary-700, #1d4ed8)"
1077
+ boxShadow: "none"
703
1078
  },
704
1079
  ".cm-activeLine": {
705
- backgroundColor: "var(--color-neutral-100, #f4f4f5)"
1080
+ backgroundColor: "transparent"
706
1081
  },
707
1082
  ".cm-gutters": {
708
1083
  backgroundColor: "var(--color-neutral-50, #fafafa)",
@@ -716,7 +1091,14 @@ var editorTheme2 = EditorView4.theme(
716
1091
  color: "var(--color-primary-700, #1d4ed8)",
717
1092
  fontWeight: "600"
718
1093
  },
719
- '.cm-tooltip-autocomplete > ul > li[aria-selected="true"] .cm-completionIcon-chord-used + .cm-completionLabel': {
1094
+ ".cm-tooltip-autocomplete ul li[aria-selected] .cm-completionIcon-chord-used + .cm-completionLabel": {
1095
+ color: "var(--color-white, #ffffff)"
1096
+ },
1097
+ ".cm-tooltip-autocomplete ul li[aria-selected]": {
1098
+ backgroundColor: "var(--color-primary-600, #2563eb)",
1099
+ color: "var(--color-white, #ffffff)"
1100
+ },
1101
+ ".cm-tooltip-autocomplete ul li[aria-selected] .cm-completionLabel, .cm-tooltip-autocomplete ul li[aria-selected] .cm-completionDetail, .cm-tooltip-autocomplete ul li[aria-selected] .cm-completionMatchedText": {
720
1102
  color: "var(--color-white, #ffffff)"
721
1103
  },
722
1104
  ".cm-lintRange-info": {
@@ -736,6 +1118,10 @@ var editorTheme2 = EditorView4.theme(
736
1118
  backgroundSize: "6px 3px",
737
1119
  backgroundPosition: "left bottom",
738
1120
  backgroundRepeat: "no-repeat"
1121
+ },
1122
+ ".chord-search-panel": {
1123
+ borderColor: "var(--color-neutral-300, #d4d4d8)",
1124
+ backgroundColor: "var(--color-white, #ffffff)"
739
1125
  }
740
1126
  },
741
1127
  { dark: false }
@@ -756,6 +1142,198 @@ function themeExtension(name) {
756
1142
  return [baseTheme, ...name === "dark" ? darkTheme : lightTheme];
757
1143
  }
758
1144
 
1145
+ // src/search-panel.ts
1146
+ import {
1147
+ closeSearchPanel,
1148
+ findNext,
1149
+ findPrevious,
1150
+ getSearchQuery,
1151
+ replaceAll,
1152
+ replaceNext,
1153
+ SearchQuery,
1154
+ setSearchQuery
1155
+ } from "@codemirror/search";
1156
+ var defaultSearchLabels = {
1157
+ search: "Find",
1158
+ replace: "Replace",
1159
+ next: "Next match",
1160
+ previous: "Previous match",
1161
+ replaceOne: "Replace",
1162
+ replaceAll: "Replace all",
1163
+ close: "Close search",
1164
+ toggleReplace: "Show replace field",
1165
+ hideReplace: "Hide replace field",
1166
+ preserveCase: "Preserve case",
1167
+ caseSensitive: "Match case",
1168
+ wholeWord: "Whole word"
1169
+ };
1170
+ function createSearchPanel(view, labels = defaultSearchLabels) {
1171
+ const panel = document.createElement("form");
1172
+ panel.className = "chord-search-panel";
1173
+ panel.addEventListener("submit", (event) => event.preventDefault());
1174
+ const searchInput = createInput("chord-search-input", labels.search);
1175
+ const replaceInput = createInput("chord-replace-input", labels.replace);
1176
+ const nextButton = createButton("chord-search-next", labels.next, "\u203A");
1177
+ const previousButton = createButton("chord-search-previous", labels.previous, "\u2039");
1178
+ const replaceButton = createButton("chord-search-replace", labels.replaceOne, "\u21B5");
1179
+ const replaceAllButton = createButton("chord-search-replace-all", labels.replaceAll, "\u21B5\xD7");
1180
+ const replaceToggle = createButton("chord-search-toggle-replace", labels.toggleReplace, "\u2194");
1181
+ const closeButton = createButton("chord-search-close", labels.close, "\xD7");
1182
+ const matchCount = document.createElement("span");
1183
+ matchCount.className = "chord-search-match-count";
1184
+ matchCount.setAttribute("aria-live", "polite");
1185
+ const preserveCase = createToggle("chord-search-preserve-case", labels.preserveCase, "AB");
1186
+ const caseSensitive = createToggle("chord-search-case", labels.caseSensitive, "Aa");
1187
+ const wholeWord = createToggle("chord-search-whole-word", labels.wholeWord, "ab");
1188
+ searchInput.wrapper.classList.add("chord-search-input-with-options");
1189
+ searchInput.wrapper.append(caseSensitive.button, wholeWord.button);
1190
+ replaceInput.wrapper.classList.add("chord-search-input-with-options");
1191
+ replaceInput.wrapper.append(preserveCase.button);
1192
+ const controls = document.createElement("div");
1193
+ controls.className = "chord-search-controls";
1194
+ const replaceRow = document.createElement("div");
1195
+ replaceRow.className = "chord-search-replace-row";
1196
+ replaceRow.hidden = false;
1197
+ replaceRow.append(replaceInput.wrapper, replaceButton, replaceAllButton);
1198
+ const searchFields = document.createElement("div");
1199
+ searchFields.className = "chord-search-fields";
1200
+ searchFields.append(searchInput.wrapper, replaceRow);
1201
+ const navigation = document.createElement("div");
1202
+ navigation.className = "chord-search-navigation";
1203
+ navigation.append(previousButton, nextButton, closeButton);
1204
+ controls.append(replaceToggle, searchFields, matchCount, navigation);
1205
+ panel.append(controls);
1206
+ const updateQuery = () => {
1207
+ const current = getSearchQuery(view.state);
1208
+ view.dispatch({
1209
+ effects: setSearchQuery.of(new SearchQuery({
1210
+ search: searchInput.input.value,
1211
+ replace: replaceInput.input.value,
1212
+ caseSensitive: caseSensitive.input.checked,
1213
+ wholeWord: wholeWord.input.checked,
1214
+ literal: current.literal
1215
+ }))
1216
+ });
1217
+ };
1218
+ const runCommand = (command) => {
1219
+ updateQuery();
1220
+ command(view);
1221
+ searchInput.input.focus();
1222
+ };
1223
+ searchInput.input.addEventListener("input", updateQuery);
1224
+ replaceInput.input.addEventListener("input", updateQuery);
1225
+ caseSensitive.input.addEventListener("change", updateQuery);
1226
+ caseSensitive.button.addEventListener("click", () => {
1227
+ caseSensitive.input.checked = !caseSensitive.input.checked;
1228
+ updateQuery();
1229
+ });
1230
+ wholeWord.button.addEventListener("click", () => {
1231
+ wholeWord.input.checked = !wholeWord.input.checked;
1232
+ updateQuery();
1233
+ });
1234
+ preserveCase.button.addEventListener("click", () => {
1235
+ preserveCase.input.checked = !preserveCase.input.checked;
1236
+ preserveCase.button.classList.toggle("is-active", preserveCase.input.checked);
1237
+ });
1238
+ wholeWord.input.addEventListener("change", updateQuery);
1239
+ searchInput.input.addEventListener("keydown", (event) => handleKey(event, () => runCommand(findNext), () => runCommand(findPrevious)));
1240
+ replaceInput.input.addEventListener("keydown", (event) => handleKey(event, () => runCommand(findNext), () => runCommand(findPrevious)));
1241
+ nextButton.addEventListener("click", () => runCommand(findNext));
1242
+ previousButton.addEventListener("click", () => runCommand(findPrevious));
1243
+ replaceButton.addEventListener("click", () => runCommand(replaceNext));
1244
+ replaceAllButton.addEventListener("click", () => runCommand(replaceAll));
1245
+ closeButton.addEventListener("click", () => closeSearchPanel(view));
1246
+ replaceToggle.addEventListener("click", () => {
1247
+ replaceRow.hidden = !replaceRow.hidden;
1248
+ replaceToggle.classList.toggle("is-active", !replaceRow.hidden);
1249
+ replaceToggle.title = replaceRow.hidden ? labels.toggleReplace : labels.hideReplace;
1250
+ replaceToggle.setAttribute("aria-label", replaceToggle.title);
1251
+ replaceInput.input.focus();
1252
+ });
1253
+ replaceToggle.classList.add("is-active");
1254
+ replaceToggle.title = labels.hideReplace;
1255
+ replaceToggle.setAttribute("aria-label", labels.hideReplace);
1256
+ const update = () => {
1257
+ const query = getSearchQuery(view.state);
1258
+ if (document.activeElement !== searchInput.input) searchInput.input.value = query.search;
1259
+ if (document.activeElement !== replaceInput.input) replaceInput.input.value = query.replace;
1260
+ caseSensitive.input.checked = query.caseSensitive;
1261
+ wholeWord.input.checked = query.wholeWord;
1262
+ caseSensitive.button.classList.toggle("is-active", query.caseSensitive);
1263
+ wholeWord.button.classList.toggle("is-active", query.wholeWord);
1264
+ preserveCase.button.classList.toggle("is-active", preserveCase.input.checked);
1265
+ updateMatchCount(query, view, matchCount);
1266
+ };
1267
+ update();
1268
+ return {
1269
+ dom: panel,
1270
+ top: true,
1271
+ mount: () => {
1272
+ update();
1273
+ searchInput.input.focus();
1274
+ searchInput.input.select();
1275
+ },
1276
+ update: (_update) => update()
1277
+ };
1278
+ }
1279
+ function updateMatchCount(query, view, target) {
1280
+ if (!query.valid || !query.search) {
1281
+ target.textContent = "";
1282
+ return;
1283
+ }
1284
+ const matches = [];
1285
+ const cursor = query.getCursor(view.state);
1286
+ for (let match = cursor.next(); !match.done; match = cursor.next()) {
1287
+ matches.push(match.value);
1288
+ }
1289
+ if (!matches.length) {
1290
+ target.textContent = "0";
1291
+ return;
1292
+ }
1293
+ const selection = view.state.selection.main;
1294
+ const current = matches.findIndex((match) => match.from === selection.from && match.to === selection.to);
1295
+ target.textContent = `${current >= 0 ? current + 1 : 1} / ${matches.length}`;
1296
+ }
1297
+ function createInput(className, placeholder) {
1298
+ const wrapper = document.createElement("label");
1299
+ wrapper.className = `chord-search-field ${className}`;
1300
+ const input = document.createElement("input");
1301
+ input.type = "text";
1302
+ input.placeholder = placeholder;
1303
+ input.setAttribute("aria-label", placeholder);
1304
+ wrapper.append(input);
1305
+ return { wrapper, input };
1306
+ }
1307
+ function createButton(className, label, text = label) {
1308
+ const button = document.createElement("button");
1309
+ button.type = "button";
1310
+ button.className = `chord-search-button ${className}`;
1311
+ button.textContent = text;
1312
+ button.title = label;
1313
+ button.setAttribute("aria-label", label);
1314
+ return button;
1315
+ }
1316
+ function createToggle(className, label, text) {
1317
+ const button = document.createElement("button");
1318
+ button.type = "button";
1319
+ button.className = `chord-search-option chord-search-toggle ${className}`;
1320
+ button.title = label;
1321
+ button.setAttribute("aria-label", label);
1322
+ const input = document.createElement("input");
1323
+ input.type = "checkbox";
1324
+ input.tabIndex = -1;
1325
+ input.setAttribute("aria-hidden", "true");
1326
+ const textElement = document.createElement("span");
1327
+ textElement.textContent = text;
1328
+ button.append(input, textElement);
1329
+ return { button, input };
1330
+ }
1331
+ function handleKey(event, next, previous) {
1332
+ if (event.key !== "Enter") return;
1333
+ event.preventDefault();
1334
+ event.shiftKey ? previous() : next();
1335
+ }
1336
+
759
1337
  // src/index.ts
760
1338
  function createChordProEditor(options) {
761
1339
  const themeCompartment = new Compartment();
@@ -767,17 +1345,17 @@ function createChordProEditor(options) {
767
1345
  (update.view.hasFocus ? options.onFocus : options.onBlur)?.();
768
1346
  }
769
1347
  });
1348
+ const lintExtensions = createLintExtensions(options.lintLabels ?? (() => defaultLintLabels));
770
1349
  const state = EditorState2.create({
771
1350
  doc: options.doc ?? "",
772
1351
  extensions: [
773
1352
  basicSetup(),
774
1353
  chordPro(),
775
1354
  chordProFolding,
776
- duplicateDirectiveLinter,
777
- parserWarningLinter,
778
- chordNotationLinter,
1355
+ ...lintExtensions,
779
1356
  chordProAutocomplete(),
780
1357
  themeCompartment.of(themeExtension(options.theme ?? "light")),
1358
+ search({ top: true, createPanel: (view2) => createSearchPanel(view2, options.searchLabels?.()) }),
781
1359
  updateListener,
782
1360
  ...options.extensions ?? []
783
1361
  ]
@@ -800,6 +1378,9 @@ function createChordProEditor(options) {
800
1378
  openCompletionList: () => {
801
1379
  openCompletionList(view);
802
1380
  },
1381
+ openSearch: () => {
1382
+ openSearchPanel(view);
1383
+ },
803
1384
  getChordNotationSuggestions: () => getChordNotationSuggestions(view.state.doc.toString()),
804
1385
  normalizeChordNotation: () => {
805
1386
  const content = view.state.doc.toString();
@@ -812,6 +1393,7 @@ function createChordProEditor(options) {
812
1393
  }
813
1394
  return normalization.suggestions;
814
1395
  },
1396
+ refreshLint: () => view.dispatch({}),
815
1397
  focus: () => view.focus(),
816
1398
  destroy: () => view.destroy()
817
1399
  };