@nectary/components 5.45.4 → 5.47.0

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/bundle.js CHANGED
@@ -1620,12 +1620,19 @@ const regEmoji = new RegExp(`(?<emoji>\\p{RI}\\p{RI}|${regEmojiSeq}(?:\\u200D${r
1620
1620
  const regUList = /^(?<indent>[\t ]*?)[*+-][\t ]+(?<ultext>.*?)[\t ]*?$/;
1621
1621
  const regOList = /^(?<indent>[\t ]*?)\d+\.[\t ]+(?<oltext>.*?)[\t ]*?$/;
1622
1622
  const regEscapedChars = /\\(?<escaped>[\\\*_\[\]`~\{\}])/;
1623
- const allRegs = [
1624
- regEscapedChars,
1623
+ const buildEscapedCharsReg = (extraChars) => {
1624
+ if (extraChars === "") {
1625
+ return regEscapedChars;
1626
+ }
1627
+ const classChars = Array.from(new Set(extraChars)).map((c) => c.replace(/[\\\]^-]/g, "\\$&")).join("");
1628
+ return new RegExp(regEscapedChars.source.replace(/\]\)$/, `${classChars}])`));
1629
+ };
1630
+ const buildAllRegs = (chipReg, escapeReg = regEscapedChars) => [
1631
+ escapeReg,
1625
1632
  regCodeTag,
1626
1633
  regButtonPlaceholder,
1627
1634
  regLink,
1628
- regChip,
1635
+ chipReg,
1629
1636
  regEm3Star,
1630
1637
  regEm2Star,
1631
1638
  regEm1Star,
@@ -1638,12 +1645,15 @@ const allRegs = [
1638
1645
  const isEmojiString = (data2) => regEmoji.test(data2);
1639
1646
  const excludeRegs = (regs, ...excluding) => regs.filter((r) => !excluding.includes(r));
1640
1647
  const matchClosest = (regs, line) => {
1641
- return regs.map((reg) => reg.exec(line)).reduce((res, match) => {
1642
- if (match === null) {
1648
+ return regs.map((reg) => {
1649
+ const match = reg.exec(line);
1650
+ return match === null ? null : { reg, match };
1651
+ }).reduce((res, current) => {
1652
+ if (current === null) {
1643
1653
  return res;
1644
1654
  }
1645
- if (res === null || res.index > match.index) {
1646
- return match;
1655
+ if (res === null || res.match.index > current.match.index) {
1656
+ return current;
1647
1657
  }
1648
1658
  return res;
1649
1659
  }, null);
@@ -1653,10 +1663,11 @@ const INITIAL_CONTEXT = {
1653
1663
  isItalic: false,
1654
1664
  isStrikethrough: false
1655
1665
  };
1656
- const createLineParser = (visitor) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
1666
+ const createLineParser = (visitor, chipReg) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
1657
1667
  let line = md;
1658
- let match = null;
1659
- while ((match = matchClosest(regs, line)) !== null) {
1668
+ let result = null;
1669
+ while ((result = matchClosest(regs, line)) !== null) {
1670
+ const { reg, match } = result;
1660
1671
  const groups = match.groups;
1661
1672
  const matchedStr = match[0];
1662
1673
  if (match.index > 0) {
@@ -1680,11 +1691,12 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
1680
1691
  if (groups?.code != null) {
1681
1692
  visitor.codetag(groups.code);
1682
1693
  }
1683
- if (groups?.chip != null) {
1684
- if (visitor.linkPlaceholder(groups.chip)) {
1685
- continue;
1694
+ if (reg === chipReg) {
1695
+ const chipName = groups?.chip ?? match[1];
1696
+ if (chipName != null && !visitor.linkPlaceholder(chipName)) {
1697
+ visitor.tag(chipName);
1686
1698
  }
1687
- visitor.tag(groups.chip);
1699
+ continue;
1688
1700
  }
1689
1701
  if (groups?.emoji != null) {
1690
1702
  visitor.emoji(groups.emoji);
@@ -1722,8 +1734,9 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
1722
1734
  visitor.inline(line, context);
1723
1735
  }
1724
1736
  };
1725
- const createBlockParser = (visitor) => {
1726
- const parseLine = createLineParser(visitor);
1737
+ const createBlockParser = (visitor, chipReg, escapeReg = regEscapedChars) => {
1738
+ const parseLine = createLineParser(visitor, chipReg);
1739
+ const allRegs = buildAllRegs(chipReg, escapeReg);
1727
1740
  const listRegs = [regUList, regOList];
1728
1741
  const getListItemText = (match) => {
1729
1742
  return match.groups.ultext ?? match.groups.oltext;
@@ -1737,12 +1750,12 @@ const createBlockParser = (visitor) => {
1737
1750
  const listMatch = matchClosest(listRegs, lines[i]);
1738
1751
  const isListLine = listMatch !== null;
1739
1752
  if (isListLine) {
1740
- const groups = listMatch.groups;
1753
+ const groups = listMatch.match.groups;
1741
1754
  const isOrderedList = groups.oltext != null;
1742
1755
  mode = isOrderedList ? BLOCK_MODE_OLIST : BLOCK_MODE_ULIST;
1743
1756
  visitor.list(isOrderedList);
1744
1757
  const listItemInitialIndent = groups.indent;
1745
- let listItemLines = [getListItemText(listMatch)];
1758
+ let listItemLines = [getListItemText(listMatch.match)];
1746
1759
  visitor.listItem();
1747
1760
  for (++i; i < lines.length; ++i) {
1748
1761
  const listItemMatch = isOrderedList ? regOList.exec(lines[i]) : regUList.exec(lines[i]);
@@ -1770,8 +1783,8 @@ const createBlockParser = (visitor) => {
1770
1783
  }
1771
1784
  };
1772
1785
  };
1773
- const parseMarkdown = (md, visitor) => {
1774
- const parseBlock = createBlockParser(visitor);
1786
+ const parseMarkdown = (md, visitor, chipReg = regChip, escapeReg = regEscapedChars) => {
1787
+ const parseBlock = createBlockParser(visitor, chipReg, escapeReg);
1775
1788
  for (const block of md.trim().split(regParagraph)) {
1776
1789
  parseBlock(block.trim().split(regLinebreak));
1777
1790
  }
@@ -1939,6 +1952,11 @@ class RichText extends NectaryElement {
1939
1952
  #parseVisitor;
1940
1953
  #controller = null;
1941
1954
  #chipResolver = null;
1955
+ /* @internal
1956
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
1957
+ * the chip name). When null the default `{{name}}` form is used.
1958
+ */
1959
+ #chipPattern = null;
1942
1960
  constructor() {
1943
1961
  super();
1944
1962
  const shadowRoot = this.attachShadow();
@@ -1978,7 +1996,7 @@ class RichText extends NectaryElement {
1978
1996
  super.disconnectedCallback();
1979
1997
  }
1980
1998
  static get observedAttributes() {
1981
- return ["text", "chip-color", "chip-icon"];
1999
+ return ["text", "chip-color", "chip-icon", "chip-delimiter"];
1982
2000
  }
1983
2001
  attributeChangedCallback(name, _oldVal, _newVal) {
1984
2002
  switch (name) {
@@ -1996,6 +2014,10 @@ class RichText extends NectaryElement {
1996
2014
  this.#updateText();
1997
2015
  break;
1998
2016
  }
2017
+ case "chip-delimiter": {
2018
+ this.#updateText();
2019
+ break;
2020
+ }
1999
2021
  }
2000
2022
  }
2001
2023
  get size() {
@@ -2022,6 +2044,12 @@ class RichText extends NectaryElement {
2022
2044
  set chipIcon(value) {
2023
2045
  updateAttribute(this, "chip-icon", value);
2024
2046
  }
2047
+ set chipDelimiter(value) {
2048
+ updateAttribute(this, "chip-delimiter", value);
2049
+ }
2050
+ get chipDelimiter() {
2051
+ return getAttribute(this, "chip-delimiter");
2052
+ }
2025
2053
  get chipResolver() {
2026
2054
  return this.#chipResolver;
2027
2055
  }
@@ -2030,6 +2058,31 @@ class RichText extends NectaryElement {
2030
2058
  this.#parseVisitor.updateChipResolver(value);
2031
2059
  this.#updateText();
2032
2060
  }
2061
+ set chipPattern(pattern) {
2062
+ this.#chipPattern = pattern;
2063
+ this.#updateText();
2064
+ }
2065
+ get chipPattern() {
2066
+ return this.#chipPattern;
2067
+ }
2068
+ #getChipPattern() {
2069
+ if (this.#chipPattern !== null) {
2070
+ return this.#chipPattern;
2071
+ }
2072
+ const delimiter = this.chipDelimiter;
2073
+ if (delimiter !== null && delimiter !== "") {
2074
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2075
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
2076
+ }
2077
+ return null;
2078
+ }
2079
+ #getEscapePattern() {
2080
+ const delimiter = this.chipDelimiter;
2081
+ if (delimiter !== null && delimiter !== "") {
2082
+ return buildEscapedCharsReg(delimiter);
2083
+ }
2084
+ return void 0;
2085
+ }
2033
2086
  #handleElementClick = (e) => {
2034
2087
  const eventTarget = e.target;
2035
2088
  const elementClickEvent = new CustomEvent("-element-click");
@@ -2046,7 +2099,12 @@ class RichText extends NectaryElement {
2046
2099
  this.#wrapper.innerHTML = "";
2047
2100
  } else {
2048
2101
  this.#wrapper.replaceChildren(
2049
- parseMarkdown(text, this.#parseVisitor.createVisitor())
2102
+ parseMarkdown(
2103
+ text,
2104
+ this.#parseVisitor.createVisitor(),
2105
+ this.#getChipPattern() ?? void 0,
2106
+ this.#getEscapePattern()
2107
+ )
2050
2108
  );
2051
2109
  }
2052
2110
  }
@@ -13719,18 +13777,28 @@ const insertEmoji = ($root, emojiChar, range) => {
13719
13777
  )
13720
13778
  };
13721
13779
  };
13722
- const insertText = ($root, data2, range) => {
13780
+ const insertText = ($root, data2, range, chip) => {
13723
13781
  if (data2 !== null && isEmojiString(data2)) {
13724
13782
  return insertEmoji($root, data2, range);
13725
13783
  }
13784
+ const matchTagAtEnd = (text) => {
13785
+ const tagRe = chip?.pattern != null ? new RegExp(`${chip.pattern.source}$`) : new RegExp("(?<!\\\\)\\{\\{([a-zA-Z0-9_-]+)\\}\\}$");
13786
+ const match = text.match(tagRe);
13787
+ if (match !== null && match[1] != null) {
13788
+ return { name: match[1], length: match[0].length };
13789
+ }
13790
+ return null;
13791
+ };
13726
13792
  if (
13727
13793
  // Is collapsed range
13728
13794
  range.startContainer === range.endContainer && range.startOffset === range.endOffset && // Is non-empty text node
13729
13795
  isTextNode(range.startContainer) && !isEmptyText(range.startContainer.nodeValue)
13730
13796
  ) {
13731
13797
  const $inline2 = getParentInline(range.startContainer);
13732
- const isHotTextWhitespace = range.startOffset === range.startContainer.length && data2 === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
13733
- if (!isHotTextWhitespace) {
13798
+ const isAtEnd = range.startOffset === range.startContainer.length;
13799
+ const isHotTextWhitespace = isAtEnd && data2 === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
13800
+ const completesTag = isAtEnd && data2 === TEXT_WHITESPACE && matchTagAtEnd(range.startContainer.nodeValue ?? "") !== null;
13801
+ if (!isHotTextWhitespace && !completesTag) {
13734
13802
  return DEFAULT_ACTION_RESULT;
13735
13803
  }
13736
13804
  }
@@ -13779,11 +13847,16 @@ const insertText = ($root, data2, range) => {
13779
13847
  };
13780
13848
  }
13781
13849
  const content2 = $text.nodeValue;
13782
- const tagMatch = content2.match(/\{\{([a-zA-Z0-9_-]+)\}\}$/);
13783
- if (tagMatch !== null) {
13784
- const text = tagMatch[1];
13785
- const tagStartPos = content2.length - tagMatch[0].length;
13786
- const $tag = createTag(text, $root.ownerDocument);
13850
+ const tag = matchTagAtEnd(content2);
13851
+ if (tag !== null) {
13852
+ const tagStartPos = content2.length - tag.length;
13853
+ const options = chip?.resolve?.(tag.name);
13854
+ const $tag = createTag(
13855
+ tag.name,
13856
+ $root.ownerDocument,
13857
+ options?.color,
13858
+ options?.icon
13859
+ );
13787
13860
  const $space = createInlineWithText(data2, $root.ownerDocument);
13788
13861
  if (tagStartPos === 0) {
13789
13862
  getParentTextBlock($inline).replaceChild($tag, $inline);
@@ -13814,10 +13887,10 @@ const insertText = ($root, data2, range) => {
13814
13887
  range: createCollapsedRange(cursor)
13815
13888
  };
13816
13889
  };
13817
- const insertFromPaste = (data2, range, visitor) => {
13890
+ const insertFromPaste = (data2, range, visitor, parse) => {
13818
13891
  const cursor = removeContentInRange(range);
13819
13892
  const { $text, $inline, offset, isAfterInline } = cursor;
13820
- const $mdFragment = parseMarkdown(data2, visitor);
13893
+ const $mdFragment = parseMarkdown(data2, visitor, parse?.chipReg, parse?.escapeReg);
13821
13894
  if ($mdFragment.childNodes.length === 0) {
13822
13895
  return {
13823
13896
  prevent: true,
@@ -14008,7 +14081,7 @@ const serializeDescriptorReducer = (range) => (state, $n) => {
14008
14081
  if (isChip($n)) {
14009
14082
  const text2 = $n.text ?? "";
14010
14083
  if (text2.length > 0) {
14011
- state.push({ isTag: true, text: `{{${text2}}}` });
14084
+ state.push({ isTag: true, text: currentChipFormat(text2) });
14012
14085
  }
14013
14086
  return state;
14014
14087
  }
@@ -14232,13 +14305,26 @@ const serializeRoot = ($root, range) => {
14232
14305
  flushParagraphChunks();
14233
14306
  return chunks.join(MD_PARAGRAPH_JOIN);
14234
14307
  };
14235
- const serializeMarkdown = ($root, range) => {
14236
- if (range !== null) {
14237
- const aCursor = createIncomingCursorFromNodeWithOffset(range.startContainer, range.startOffset);
14238
- const bCursor = createIncomingCursorFromNodeWithOffset(range.endContainer, range.endOffset);
14239
- return serializeRoot($root, [aCursor, bCursor]);
14308
+ const defaultChipFormat = (name) => `{{${name}}}`;
14309
+ let currentChipFormat = defaultChipFormat;
14310
+ const serializeMarkdown = ($root, range, chipFormat) => {
14311
+ currentChipFormat = chipFormat ?? defaultChipFormat;
14312
+ try {
14313
+ if (range !== null) {
14314
+ const aCursor = createIncomingCursorFromNodeWithOffset(
14315
+ range.startContainer,
14316
+ range.startOffset
14317
+ );
14318
+ const bCursor = createIncomingCursorFromNodeWithOffset(
14319
+ range.endContainer,
14320
+ range.endOffset
14321
+ );
14322
+ return serializeRoot($root, [aCursor, bCursor]);
14323
+ }
14324
+ return serializeRoot($root, null);
14325
+ } finally {
14326
+ currentChipFormat = defaultChipFormat;
14240
14327
  }
14241
- return serializeRoot($root, null);
14242
14328
  };
14243
14329
  const createParseVisitor = (doc) => {
14244
14330
  let emojiBaseUrl = null;
@@ -14395,6 +14481,16 @@ class RichTextarea extends NectaryElement {
14395
14481
  * It is used to resolve the color and icon of a chip.
14396
14482
  */
14397
14483
  #chipResolver = null;
14484
+ /* @internal
14485
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
14486
+ * the chip name). When null the default `{{name}}` form is used.
14487
+ */
14488
+ #chipPattern = null;
14489
+ /* @internal
14490
+ * Formats a chip back to markdown when serialising. When null chips are
14491
+ * written as `{{name}}`.
14492
+ */
14493
+ #chipFormat = null;
14398
14494
  constructor() {
14399
14495
  super();
14400
14496
  const shadowRoot = this.attachShadow({ delegatesFocus: true });
@@ -14467,7 +14563,8 @@ class RichTextarea extends NectaryElement {
14467
14563
  "placeholder",
14468
14564
  "aria-label",
14469
14565
  "chip-color",
14470
- "chip-icon"
14566
+ "chip-icon",
14567
+ "chip-delimiter"
14471
14568
  ];
14472
14569
  }
14473
14570
  attributeChangedCallback(name, oldVal, newVal) {
@@ -14496,6 +14593,12 @@ class RichTextarea extends NectaryElement {
14496
14593
  this.#updateChipIcons(newVal);
14497
14594
  break;
14498
14595
  }
14596
+ case "chip-delimiter": {
14597
+ if (this.isConnected === true) {
14598
+ this.#dispatchChangeEvent();
14599
+ }
14600
+ break;
14601
+ }
14499
14602
  }
14500
14603
  }
14501
14604
  set value(value) {
@@ -14534,6 +14637,12 @@ class RichTextarea extends NectaryElement {
14534
14637
  get chipIcon() {
14535
14638
  return getAttribute(this, "chip-icon");
14536
14639
  }
14640
+ set chipDelimiter(value) {
14641
+ updateAttribute(this, "chip-delimiter", value);
14642
+ }
14643
+ get chipDelimiter() {
14644
+ return getAttribute(this, "chip-delimiter");
14645
+ }
14537
14646
  set chipResolver(resolver) {
14538
14647
  this.#chipResolver = resolver;
14539
14648
  this.#parseVisitor.updateChipResolver(resolver);
@@ -14542,6 +14651,49 @@ class RichTextarea extends NectaryElement {
14542
14651
  get chipResolver() {
14543
14652
  return this.#chipResolver;
14544
14653
  }
14654
+ set chipPattern(pattern) {
14655
+ this.#chipPattern = pattern;
14656
+ }
14657
+ get chipPattern() {
14658
+ return this.#chipPattern;
14659
+ }
14660
+ set chipFormat(format) {
14661
+ this.#chipFormat = format;
14662
+ if (this.isConnected === true) {
14663
+ this.#dispatchChangeEvent();
14664
+ }
14665
+ }
14666
+ get chipFormat() {
14667
+ return this.#chipFormat;
14668
+ }
14669
+ #getChipPattern() {
14670
+ if (this.#chipPattern !== null) {
14671
+ return this.#chipPattern;
14672
+ }
14673
+ const delimiter = this.chipDelimiter;
14674
+ if (delimiter !== null && delimiter !== "") {
14675
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14676
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
14677
+ }
14678
+ return null;
14679
+ }
14680
+ #getChipFormat() {
14681
+ if (this.#chipFormat !== null) {
14682
+ return this.#chipFormat;
14683
+ }
14684
+ const delimiter = this.chipDelimiter;
14685
+ if (delimiter !== null && delimiter !== "") {
14686
+ return (name) => `${delimiter}${name}${delimiter}`;
14687
+ }
14688
+ return null;
14689
+ }
14690
+ #getEscapePattern() {
14691
+ const delimiter = this.chipDelimiter;
14692
+ if (delimiter !== null && delimiter !== "") {
14693
+ return buildEscapedCharsReg(delimiter);
14694
+ }
14695
+ return void 0;
14696
+ }
14545
14697
  get focusable() {
14546
14698
  return true;
14547
14699
  }
@@ -14718,6 +14870,13 @@ class RichTextarea extends NectaryElement {
14718
14870
  }
14719
14871
  this.#handleActionResult(handleResult);
14720
14872
  };
14873
+ #resolveChipOptions = (name) => {
14874
+ const resolved = this.#chipResolver?.(name);
14875
+ return {
14876
+ color: resolved?.color ?? this.getAttribute("chip-color") ?? void 0,
14877
+ icon: resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0
14878
+ };
14879
+ };
14721
14880
  #handleInput(inputType, range, text, href) {
14722
14881
  if (range === null) {
14723
14882
  return { prevent: true, range: null };
@@ -14743,17 +14902,17 @@ class RichTextarea extends NectaryElement {
14743
14902
  }
14744
14903
  case "insertReplacementText":
14745
14904
  case "insertText": {
14746
- return insertText(this.#$input, text, range);
14905
+ return insertText(this.#$input, text, range, {
14906
+ pattern: this.#getChipPattern(),
14907
+ resolve: this.#resolveChipOptions
14908
+ });
14747
14909
  }
14748
14910
  case "insertLink": {
14749
14911
  return insertLink(this.#$input, text, href, range);
14750
14912
  }
14751
14913
  case "insertMention":
14752
14914
  case "insertChip": {
14753
- const resolved = this.#chipResolver?.(text);
14754
- const color = resolved?.color ?? this.getAttribute("chip-color") ?? void 0;
14755
- const icon = resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0;
14756
- return insertChip(this.#$input, text, range, { color, icon });
14915
+ return insertChip(this.#$input, text, range, this.#resolveChipOptions(text));
14757
14916
  }
14758
14917
  // case 'formatUnderline':
14759
14918
  case "formatItalic":
@@ -14812,7 +14971,7 @@ class RichTextarea extends NectaryElement {
14812
14971
  this.#dispatchChangeEvent();
14813
14972
  };
14814
14973
  #dispatchChangeEvent() {
14815
- const value = serializeMarkdown(this.#$input, null);
14974
+ const value = serializeMarkdown(this.#$input, null, this.#getChipFormat());
14816
14975
  if (value === this.#prevDispatchedValue) {
14817
14976
  return;
14818
14977
  }
@@ -14855,7 +15014,12 @@ class RichTextarea extends NectaryElement {
14855
15014
  this.#cachedRange = null;
14856
15015
  this.#prevDispatchedValue = value;
14857
15016
  this.#$input.replaceChildren(
14858
- parseMarkdown(value, this.#parseVisitor.createVisitor())
15017
+ parseMarkdown(
15018
+ value,
15019
+ this.#parseVisitor.createVisitor(),
15020
+ this.#getChipPattern() ?? void 0,
15021
+ this.#getEscapePattern()
15022
+ )
14859
15023
  );
14860
15024
  this.#updateEditorEmptyClass();
14861
15025
  }
@@ -14865,7 +15029,10 @@ class RichTextarea extends NectaryElement {
14865
15029
  return;
14866
15030
  }
14867
15031
  if (e.clipboardData != null) {
14868
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
15032
+ e.clipboardData.setData(
15033
+ "text/plain",
15034
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
15035
+ );
14869
15036
  }
14870
15037
  };
14871
15038
  #onCut = (e) => {
@@ -14874,7 +15041,10 @@ class RichTextarea extends NectaryElement {
14874
15041
  return;
14875
15042
  }
14876
15043
  if (e.clipboardData != null) {
14877
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
15044
+ e.clipboardData.setData(
15045
+ "text/plain",
15046
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
15047
+ );
14878
15048
  }
14879
15049
  this.#handleActionResult(
14880
15050
  deleteContentBackward(this.#$input, this.#cachedRange)
@@ -14890,7 +15060,11 @@ class RichTextarea extends NectaryElement {
14890
15060
  insertFromPaste(
14891
15061
  pasteValue,
14892
15062
  this.#cachedRange,
14893
- this.#parseVisitor.createVisitor()
15063
+ this.#parseVisitor.createVisitor(),
15064
+ {
15065
+ chipReg: this.#getChipPattern() ?? void 0,
15066
+ escapeReg: this.#getEscapePattern()
15067
+ }
14894
15068
  )
14895
15069
  );
14896
15070
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/components",
3
- "version": "5.45.4",
3
+ "version": "5.47.0",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -21,6 +21,10 @@ export declare class RichText extends NectaryElement {
21
21
  set chipColor(value: string | null);
22
22
  get chipIcon(): string | null;
23
23
  set chipIcon(value: string | null);
24
+ set chipDelimiter(value: string | null);
25
+ get chipDelimiter(): string | null;
24
26
  get chipResolver(): TChipResolver | null;
25
27
  set chipResolver(value: TChipResolver | null);
28
+ set chipPattern(pattern: RegExp | null);
29
+ get chipPattern(): RegExp | null;
26
30
  }
@@ -6,7 +6,7 @@ import "../rich-textarea-chip/index.js";
6
6
  import { getLiteralAttribute, updateLiteralAttribute, getAttribute, updateAttribute } from "../utils/dom.js";
7
7
  import { defineCustomElement, NectaryElement } from "../utils/element.js";
8
8
  import { getReactEventHandler } from "../utils/get-react-event-handler.js";
9
- import { parseMarkdown } from "../utils/markdown.js";
9
+ import { buildEscapedCharsReg, parseMarkdown } from "../utils/markdown.js";
10
10
  import { createParseVisitor, sizeValues } from "./utils.js";
11
11
  const templateHTML = '<style>:host{display:block;--sinch-comp-rich-text-font:var(--sinch-sys-font-body-m)}:host([size="s"]){--sinch-comp-rich-text-font:var(--sinch-sys-font-body-s)}:host([size=xs]){--sinch-comp-rich-text-font:var(--sinch-sys-font-body-xs)}:host([size=xxs]){--sinch-comp-rich-text-font:var(--sinch-sys-font-body-xxs)}#wrapper{font:var(--sinch-comp-rich-text-font);color:var(--sinch-global-color-text,var(--sinch-sys-color-text-default))}.em1{font-style:italic}.em2{font-weight:var(--sinch-ref-typography-font-weight-700)}.strikethrough{text-decoration:line-through}.link{font:var(--sinch-comp-link-default-font-initial);color:var(--sinch-comp-link-color-default-text-initial);text-decoration:underline}.link:hover{color:var(--sinch-comp-link-color-default-text-hover);text-decoration:none}.code{font:var(--sinch-comp-code-tag-font-text);line-height:inherit;font-size:inherit;border:1px solid var(--sinch-comp-code-tag-color-default-border-initial);background-color:var(--sinch-comp-code-tag-color-default-background-initial);padding:0 .25em;border-radius:var(--sinch-comp-code-tag-shape-radius)}.emoji{--sinch-global-size-icon:1em;--sinch-comp-emoji-vertical-align:-0.2em}.ol,.p,.ul{margin:0}.ol,.ul{padding-left:1.5em}.ol+.p,.p+.ol,.p+.p,.p+.ul,.ul+.p{margin-top:.5em}.li>.p+.ol,.li>.p+.ul{margin-top:0}</style><div id="wrapper"></div>';
12
12
  const template = document.createElement("template");
@@ -16,6 +16,11 @@ class RichText extends NectaryElement {
16
16
  #parseVisitor;
17
17
  #controller = null;
18
18
  #chipResolver = null;
19
+ /* @internal
20
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
21
+ * the chip name). When null the default `{{name}}` form is used.
22
+ */
23
+ #chipPattern = null;
19
24
  constructor() {
20
25
  super();
21
26
  const shadowRoot = this.attachShadow();
@@ -55,7 +60,7 @@ class RichText extends NectaryElement {
55
60
  super.disconnectedCallback();
56
61
  }
57
62
  static get observedAttributes() {
58
- return ["text", "chip-color", "chip-icon"];
63
+ return ["text", "chip-color", "chip-icon", "chip-delimiter"];
59
64
  }
60
65
  attributeChangedCallback(name, _oldVal, _newVal) {
61
66
  switch (name) {
@@ -73,6 +78,10 @@ class RichText extends NectaryElement {
73
78
  this.#updateText();
74
79
  break;
75
80
  }
81
+ case "chip-delimiter": {
82
+ this.#updateText();
83
+ break;
84
+ }
76
85
  }
77
86
  }
78
87
  get size() {
@@ -99,6 +108,12 @@ class RichText extends NectaryElement {
99
108
  set chipIcon(value) {
100
109
  updateAttribute(this, "chip-icon", value);
101
110
  }
111
+ set chipDelimiter(value) {
112
+ updateAttribute(this, "chip-delimiter", value);
113
+ }
114
+ get chipDelimiter() {
115
+ return getAttribute(this, "chip-delimiter");
116
+ }
102
117
  get chipResolver() {
103
118
  return this.#chipResolver;
104
119
  }
@@ -107,6 +122,31 @@ class RichText extends NectaryElement {
107
122
  this.#parseVisitor.updateChipResolver(value);
108
123
  this.#updateText();
109
124
  }
125
+ set chipPattern(pattern) {
126
+ this.#chipPattern = pattern;
127
+ this.#updateText();
128
+ }
129
+ get chipPattern() {
130
+ return this.#chipPattern;
131
+ }
132
+ #getChipPattern() {
133
+ if (this.#chipPattern !== null) {
134
+ return this.#chipPattern;
135
+ }
136
+ const delimiter = this.chipDelimiter;
137
+ if (delimiter !== null && delimiter !== "") {
138
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
139
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
140
+ }
141
+ return null;
142
+ }
143
+ #getEscapePattern() {
144
+ const delimiter = this.chipDelimiter;
145
+ if (delimiter !== null && delimiter !== "") {
146
+ return buildEscapedCharsReg(delimiter);
147
+ }
148
+ return void 0;
149
+ }
110
150
  #handleElementClick = (e) => {
111
151
  const eventTarget = e.target;
112
152
  const elementClickEvent = new CustomEvent("-element-click");
@@ -123,7 +163,12 @@ class RichText extends NectaryElement {
123
163
  this.#wrapper.innerHTML = "";
124
164
  } else {
125
165
  this.#wrapper.replaceChildren(
126
- parseMarkdown(text, this.#parseVisitor.createVisitor())
166
+ parseMarkdown(
167
+ text,
168
+ this.#parseVisitor.createVisitor(),
169
+ this.#getChipPattern() ?? void 0,
170
+ this.#getEscapePattern()
171
+ )
127
172
  );
128
173
  }
129
174
  }
@@ -12,10 +12,22 @@ export type TSinchRichTextProps = {
12
12
  'chip-color'?: TSinchTagColor;
13
13
  /** Default icon for chips */
14
14
  'chip-icon'?: string;
15
+ /**
16
+ * Symmetric wrapping delimiter for chips in markdown, e.g. "#" produces
17
+ * `#name#`. A shorthand that derives chipPattern; the explicit chipPattern
18
+ * property overrides it.
19
+ */
20
+ 'chip-delimiter'?: string;
15
21
  };
16
22
  export type TSinchRichTextMethods = {
17
23
  /** Resolver callback for chip icon and color based on tag name */
18
24
  chipResolver: TChipResolver | null;
25
+ /**
26
+ * Regex used to recognise a chip when parsing markdown. Capture group 1 is
27
+ * the chip name. When null the default `{{name}}` form is used. Overrides
28
+ * the `chip-delimiter` attribute.
29
+ */
30
+ chipPattern: RegExp | null;
19
31
  };
20
32
  export type TSinchRichTextEvents = {
21
33
  /** Click event handler */
@@ -21,8 +21,14 @@ export declare class RichTextarea extends NectaryElement {
21
21
  get chipColor(): string | null;
22
22
  set chipIcon(value: string | null);
23
23
  get chipIcon(): string | null;
24
+ set chipDelimiter(value: string | null);
25
+ get chipDelimiter(): string | null;
24
26
  set chipResolver(resolver: TChipResolver | null);
25
27
  get chipResolver(): TChipResolver | null;
28
+ set chipPattern(pattern: RegExp | null);
29
+ get chipPattern(): RegExp | null;
30
+ set chipFormat(format: ((name: string) => string) | null);
31
+ get chipFormat(): ((name: string) => string) | null;
26
32
  get focusable(): boolean;
27
33
  focus(): void;
28
34
  blur(): void;
@@ -4,7 +4,7 @@ import { updateAttribute, getAttribute, updateBooleanAttribute, getBooleanAttrib
4
4
  import { defineCustomElement, NectaryElement } from "../utils/element.js";
5
5
  import { isElementFocused } from "../utils/slot.js";
6
6
  import { getReactEventHandler } from "../utils/get-react-event-handler.js";
7
- import { parseMarkdown } from "../utils/markdown.js";
7
+ import { buildEscapedCharsReg, parseMarkdown } from "../utils/markdown.js";
8
8
  import { createParseVisitor, getEndRange, formatList, handleEmojiMousedown, removeChip, setBrowserCaret, formatOutdent, formatIndent, formatInline, insertChip, insertLink, insertText, insertLineBreak, deleteContentBackward, serializeMarkdown, getSelectionInfo, isSelectionEqual, insertFromPaste, isEditorEmpty } from "./utils.js";
9
9
  const templateHTML = '<style>:host{display:block}#wrapper{display:flex;flex-direction:column;position:relative;width:100%;box-sizing:border-box;background-color:var(--sinch-comp-textarea-color-default-background-initial);border-radius:var(--sinch-local-shape-radius);overflow:hidden;--sinch-local-shape-radius:var(--sinch-comp-textarea-shape-radius)}#input-wrapper{position:relative;padding:8px 10px 8px 12px;box-sizing:border-box}#input{font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-initial);caret-color:var(--sinch-comp-textarea-color-default-text-initial);white-space:pre-wrap;overflow-wrap:break-word;border:none;outline:0}#placeholder{display:none;position:absolute;left:0;top:0;font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-placeholder);padding:8px 10px 8px 12px;pointer-events:none;user-select:none}#input.empty+#placeholder{display:block}#border{position:absolute;border:1px solid var(--sinch-comp-textarea-color-default-border-initial);border-radius:var(--sinch-local-shape-radius);inset:0;pointer-events:none}:host([invalid]) #border{border-color:var(--sinch-comp-textarea-color-invalid-border-initial)}:host([disabled]){color:var(--sinch-comp-textarea-color-disabled-text-initial);-webkit-text-fill-color:var(--sinch-comp-textarea-color-disabled-text-initial)}:host([disabled]) #border{border-color:var(--sinch-comp-textarea-color-disabled-border-initial)}:host(:not([disabled])) #input-wrapper:focus-within~#border{border-color:var(--sinch-comp-textarea-color-default-border-focus);border-width:2px}.oli,.p,.uli{margin:0}.oli.l0,.uli.l0{margin-left:6px}.oli.l1,.uli.l1{margin-left:36px}.oli.l2,.uli.l2{margin-left:64px}.oli.l3,.uli.l3{margin-left:92px}.oli.l4,.uli.l4{margin-left:120px}.uli.l0{counter-reset:list-0 list-1 list-2 list-3 list-4}.uli.l1{counter-reset:list-1 list-2 list-3 list-4}.uli.l2{counter-reset:list-2 list-3 list-4}.uli.l3{counter-reset:list-3 list-4}.uli.l4{counter-reset:list-4}.oli.l0{counter-reset:list-1 list-2 list-3 list-4}.oli.l1{counter-reset:list-2 list-3 list-4}.oli.l2{counter-reset:list-3 list-4}.oli.l3{counter-reset:list-4}.oli.l0::before{counter-increment:list-0;content:counter(list-0,decimal) ". "}.oli.l1::before{counter-increment:list-1;content:counter(list-1,lower-alpha) ". "}.oli.l2::before{counter-increment:list-2;content:counter(list-2,lower-roman) ". "}.oli.l3::before{counter-increment:list-3;content:counter(list-3,decimal) ". "}.oli.l4::before{counter-increment:list-4;content:counter(list-4,lower-alpha) ". "}.oli.block,.oli:first-of-type,.p+.oli{counter-reset:list-0 list-1 list-2 list-3 list-4}.uli::before{content:"\\25CF";display:inline-block;width:16px}.oli+.p,.oli.block,.p+.oli,.p+.uli,.uli+.p,.uli.block{margin-top:.5em}.c{font:var(--sinch-comp-code-tag-font-text);font-size:inherit;line-height:inherit;color:var(--sinch-comp-code-tag-color-default-text-initial);border:1px solid var(--sinch-comp-code-tag-color-default-border-initial);background-color:var(--sinch-comp-code-tag-color-default-background-initial);padding:0 .25em;border-radius:var(--sinch-comp-code-tag-shape-radius)}.l{font:var(--sinch-comp-link-default-font-initial);color:var(--sinch-comp-link-color-default-text-initial);text-decoration:underline}sinch-rich-textarea-chip{display:inline-flex;vertical-align:middle;user-select:none}.i{font-style:italic}.b{font-weight:700}.s{text-decoration:line-through}.e{background-repeat:no-repeat;background-position:50% 50%;background-size:contain;width:1em;height:1em;vertical-align:-.2em}#top-wrapper{display:flex;flex-direction:row;align-items:center;gap:8px;padding:4px 4px 0}#top-wrapper.empty{display:none}#bottom-wrapper{display:flex;flex-direction:row;align-items:center;gap:8px;padding:0 4px 4px}#bottom-wrapper.empty{display:none}</style><div id="wrapper"><div id="top-wrapper"><slot id="top" name="top"></slot></div><div id="input-wrapper"><div id="input" contenteditable="true" role="textbox" aria-multiline="true" suppresscontenteditablewarning autocapitalize="false" autocorrect="false" autosave="false" spellcheck="false"></div><div id="placeholder"></div></div><div id="border"></div><div id="bottom-wrapper"><slot id="bottom" name="bottom"></slot></div></div>';
10
10
  const template = document.createElement("template");
@@ -32,6 +32,16 @@ class RichTextarea extends NectaryElement {
32
32
  * It is used to resolve the color and icon of a chip.
33
33
  */
34
34
  #chipResolver = null;
35
+ /* @internal
36
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
37
+ * the chip name). When null the default `{{name}}` form is used.
38
+ */
39
+ #chipPattern = null;
40
+ /* @internal
41
+ * Formats a chip back to markdown when serialising. When null chips are
42
+ * written as `{{name}}`.
43
+ */
44
+ #chipFormat = null;
35
45
  constructor() {
36
46
  super();
37
47
  const shadowRoot = this.attachShadow({ delegatesFocus: true });
@@ -104,7 +114,8 @@ class RichTextarea extends NectaryElement {
104
114
  "placeholder",
105
115
  "aria-label",
106
116
  "chip-color",
107
- "chip-icon"
117
+ "chip-icon",
118
+ "chip-delimiter"
108
119
  ];
109
120
  }
110
121
  attributeChangedCallback(name, oldVal, newVal) {
@@ -133,6 +144,12 @@ class RichTextarea extends NectaryElement {
133
144
  this.#updateChipIcons(newVal);
134
145
  break;
135
146
  }
147
+ case "chip-delimiter": {
148
+ if (this.isConnected === true) {
149
+ this.#dispatchChangeEvent();
150
+ }
151
+ break;
152
+ }
136
153
  }
137
154
  }
138
155
  set value(value) {
@@ -171,6 +188,12 @@ class RichTextarea extends NectaryElement {
171
188
  get chipIcon() {
172
189
  return getAttribute(this, "chip-icon");
173
190
  }
191
+ set chipDelimiter(value) {
192
+ updateAttribute(this, "chip-delimiter", value);
193
+ }
194
+ get chipDelimiter() {
195
+ return getAttribute(this, "chip-delimiter");
196
+ }
174
197
  set chipResolver(resolver) {
175
198
  this.#chipResolver = resolver;
176
199
  this.#parseVisitor.updateChipResolver(resolver);
@@ -179,6 +202,49 @@ class RichTextarea extends NectaryElement {
179
202
  get chipResolver() {
180
203
  return this.#chipResolver;
181
204
  }
205
+ set chipPattern(pattern) {
206
+ this.#chipPattern = pattern;
207
+ }
208
+ get chipPattern() {
209
+ return this.#chipPattern;
210
+ }
211
+ set chipFormat(format) {
212
+ this.#chipFormat = format;
213
+ if (this.isConnected === true) {
214
+ this.#dispatchChangeEvent();
215
+ }
216
+ }
217
+ get chipFormat() {
218
+ return this.#chipFormat;
219
+ }
220
+ #getChipPattern() {
221
+ if (this.#chipPattern !== null) {
222
+ return this.#chipPattern;
223
+ }
224
+ const delimiter = this.chipDelimiter;
225
+ if (delimiter !== null && delimiter !== "") {
226
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
227
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
228
+ }
229
+ return null;
230
+ }
231
+ #getChipFormat() {
232
+ if (this.#chipFormat !== null) {
233
+ return this.#chipFormat;
234
+ }
235
+ const delimiter = this.chipDelimiter;
236
+ if (delimiter !== null && delimiter !== "") {
237
+ return (name) => `${delimiter}${name}${delimiter}`;
238
+ }
239
+ return null;
240
+ }
241
+ #getEscapePattern() {
242
+ const delimiter = this.chipDelimiter;
243
+ if (delimiter !== null && delimiter !== "") {
244
+ return buildEscapedCharsReg(delimiter);
245
+ }
246
+ return void 0;
247
+ }
182
248
  get focusable() {
183
249
  return true;
184
250
  }
@@ -355,6 +421,13 @@ class RichTextarea extends NectaryElement {
355
421
  }
356
422
  this.#handleActionResult(handleResult);
357
423
  };
424
+ #resolveChipOptions = (name) => {
425
+ const resolved = this.#chipResolver?.(name);
426
+ return {
427
+ color: resolved?.color ?? this.getAttribute("chip-color") ?? void 0,
428
+ icon: resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0
429
+ };
430
+ };
358
431
  #handleInput(inputType, range, text, href) {
359
432
  if (range === null) {
360
433
  return { prevent: true, range: null };
@@ -380,17 +453,17 @@ class RichTextarea extends NectaryElement {
380
453
  }
381
454
  case "insertReplacementText":
382
455
  case "insertText": {
383
- return insertText(this.#$input, text, range);
456
+ return insertText(this.#$input, text, range, {
457
+ pattern: this.#getChipPattern(),
458
+ resolve: this.#resolveChipOptions
459
+ });
384
460
  }
385
461
  case "insertLink": {
386
462
  return insertLink(this.#$input, text, href, range);
387
463
  }
388
464
  case "insertMention":
389
465
  case "insertChip": {
390
- const resolved = this.#chipResolver?.(text);
391
- const color = resolved?.color ?? this.getAttribute("chip-color") ?? void 0;
392
- const icon = resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0;
393
- return insertChip(this.#$input, text, range, { color, icon });
466
+ return insertChip(this.#$input, text, range, this.#resolveChipOptions(text));
394
467
  }
395
468
  // case 'formatUnderline':
396
469
  case "formatItalic":
@@ -449,7 +522,7 @@ class RichTextarea extends NectaryElement {
449
522
  this.#dispatchChangeEvent();
450
523
  };
451
524
  #dispatchChangeEvent() {
452
- const value = serializeMarkdown(this.#$input, null);
525
+ const value = serializeMarkdown(this.#$input, null, this.#getChipFormat());
453
526
  if (value === this.#prevDispatchedValue) {
454
527
  return;
455
528
  }
@@ -492,7 +565,12 @@ class RichTextarea extends NectaryElement {
492
565
  this.#cachedRange = null;
493
566
  this.#prevDispatchedValue = value;
494
567
  this.#$input.replaceChildren(
495
- parseMarkdown(value, this.#parseVisitor.createVisitor())
568
+ parseMarkdown(
569
+ value,
570
+ this.#parseVisitor.createVisitor(),
571
+ this.#getChipPattern() ?? void 0,
572
+ this.#getEscapePattern()
573
+ )
496
574
  );
497
575
  this.#updateEditorEmptyClass();
498
576
  }
@@ -502,7 +580,10 @@ class RichTextarea extends NectaryElement {
502
580
  return;
503
581
  }
504
582
  if (e.clipboardData != null) {
505
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
583
+ e.clipboardData.setData(
584
+ "text/plain",
585
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
586
+ );
506
587
  }
507
588
  };
508
589
  #onCut = (e) => {
@@ -511,7 +592,10 @@ class RichTextarea extends NectaryElement {
511
592
  return;
512
593
  }
513
594
  if (e.clipboardData != null) {
514
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
595
+ e.clipboardData.setData(
596
+ "text/plain",
597
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
598
+ );
515
599
  }
516
600
  this.#handleActionResult(
517
601
  deleteContentBackward(this.#$input, this.#cachedRange)
@@ -527,7 +611,11 @@ class RichTextarea extends NectaryElement {
527
611
  insertFromPaste(
528
612
  pasteValue,
529
613
  this.#cachedRange,
530
- this.#parseVisitor.createVisitor()
614
+ this.#parseVisitor.createVisitor(),
615
+ {
616
+ chipReg: this.#getChipPattern() ?? void 0,
617
+ escapeReg: this.#getEscapePattern()
618
+ }
531
619
  )
532
620
  );
533
621
  };
@@ -24,6 +24,12 @@ export type TSinchRichTextareaProps = {
24
24
  'chip-color'?: TSinchTagColor;
25
25
  /** Default icon for chips/tags */
26
26
  'chip-icon'?: string;
27
+ /**
28
+ * Symmetric wrapping delimiter for chips in markdown, e.g. "#" produces
29
+ * `#name#`. A shorthand that derives chipPattern/chipFormat; the explicit
30
+ * chipPattern/chipFormat properties override it.
31
+ */
32
+ 'chip-delimiter'?: string;
27
33
  /** Accessible label for the rich textarea */
28
34
  'aria-label': string;
29
35
  };
@@ -43,6 +49,16 @@ export type TSinchRichTextareaMethods = {
43
49
  getCaretRect(): DOMRect | null;
44
50
  /** Resolver callback for chip icon and color based on tag name */
45
51
  chipResolver: TChipResolver | null;
52
+ /**
53
+ * Regex used to recognise a chip when parsing markdown. Capture group 1 is
54
+ * the chip name. When null the default `{{name}}` form is used.
55
+ */
56
+ chipPattern: RegExp | null;
57
+ /**
58
+ * Formats a chip back to markdown when serialising. When null chips are
59
+ * written as `{{name}}`.
60
+ */
61
+ chipFormat: ((name: string) => string) | null;
46
62
  };
47
63
  export type TSinchRichTextareaEvents = {
48
64
  /** Change value handler — fires while editing (debounced) and on blur */
@@ -28,15 +28,24 @@ export declare const insertChip: ($root: TRichTextareaRoot, text: string, range:
28
28
  icon?: string;
29
29
  }) => TActionResult;
30
30
  export declare const insertLineBreak: (range: TRange) => TActionResult;
31
- export declare const insertText: ($root: TRichTextareaRoot, data: string | null, range: TRange) => TActionResult;
32
- export declare const insertFromPaste: (data: string, range: Readonly<TRange>, visitor: TMarkdownParseVisitor) => TActionResult;
31
+ export declare const insertText: ($root: TRichTextareaRoot, data: string | null, range: TRange, chip?: {
32
+ pattern?: RegExp | null;
33
+ resolve?: (name: string) => {
34
+ color?: string;
35
+ icon?: string;
36
+ };
37
+ }) => TActionResult;
38
+ export declare const insertFromPaste: (data: string, range: Readonly<TRange>, visitor: TMarkdownParseVisitor, parse?: {
39
+ chipReg?: RegExp;
40
+ escapeReg?: RegExp;
41
+ }) => TActionResult;
33
42
  export declare const handleEmojiMousedown: ($n: Node) => TActionResult;
34
43
  export declare const getBeginRange: ($root: TRichTextareaRoot) => TRange;
35
44
  export declare const getEndRange: ($root: TRichTextareaRoot) => TRange;
36
45
  export declare const getSelectionInfo: (range: TRange) => TRichTextareaSelection;
37
46
  export declare const isSelectionEqual: (a: TRichTextareaSelection | null, b: TRichTextareaSelection | null) => boolean;
38
47
  export declare const isEditorEmpty: ($root: TRichTextareaRoot) => boolean;
39
- export declare const serializeMarkdown: ($root: TRichTextareaRoot, range: Readonly<TRange> | null) => string;
48
+ export declare const serializeMarkdown: ($root: TRichTextareaRoot, range: Readonly<TRange> | null, chipFormat?: ((name: string) => string) | null) => string;
40
49
  export declare const createParseVisitor: (doc: Document) => {
41
50
  updateEmojiBaseUrl(url: string | null): void;
42
51
  updateChipColor(color: string | null): void;
@@ -1222,18 +1222,28 @@ const insertEmoji = ($root, emojiChar, range) => {
1222
1222
  )
1223
1223
  };
1224
1224
  };
1225
- const insertText = ($root, data, range) => {
1225
+ const insertText = ($root, data, range, chip) => {
1226
1226
  if (data !== null && isEmojiString(data)) {
1227
1227
  return insertEmoji($root, data, range);
1228
1228
  }
1229
+ const matchTagAtEnd = (text) => {
1230
+ const tagRe = chip?.pattern != null ? new RegExp(`${chip.pattern.source}$`) : new RegExp("(?<!\\\\)\\{\\{([a-zA-Z0-9_-]+)\\}\\}$");
1231
+ const match = text.match(tagRe);
1232
+ if (match !== null && match[1] != null) {
1233
+ return { name: match[1], length: match[0].length };
1234
+ }
1235
+ return null;
1236
+ };
1229
1237
  if (
1230
1238
  // Is collapsed range
1231
1239
  range.startContainer === range.endContainer && range.startOffset === range.endOffset && // Is non-empty text node
1232
1240
  isTextNode(range.startContainer) && !isEmptyText(range.startContainer.nodeValue)
1233
1241
  ) {
1234
1242
  const $inline2 = getParentInline(range.startContainer);
1235
- const isHotTextWhitespace = range.startOffset === range.startContainer.length && data === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
1236
- if (!isHotTextWhitespace) {
1243
+ const isAtEnd = range.startOffset === range.startContainer.length;
1244
+ const isHotTextWhitespace = isAtEnd && data === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
1245
+ const completesTag = isAtEnd && data === TEXT_WHITESPACE && matchTagAtEnd(range.startContainer.nodeValue ?? "") !== null;
1246
+ if (!isHotTextWhitespace && !completesTag) {
1237
1247
  return DEFAULT_ACTION_RESULT;
1238
1248
  }
1239
1249
  }
@@ -1282,11 +1292,16 @@ const insertText = ($root, data, range) => {
1282
1292
  };
1283
1293
  }
1284
1294
  const content2 = $text.nodeValue;
1285
- const tagMatch = content2.match(/\{\{([a-zA-Z0-9_-]+)\}\}$/);
1286
- if (tagMatch !== null) {
1287
- const text = tagMatch[1];
1288
- const tagStartPos = content2.length - tagMatch[0].length;
1289
- const $tag = createTag(text, $root.ownerDocument);
1295
+ const tag = matchTagAtEnd(content2);
1296
+ if (tag !== null) {
1297
+ const tagStartPos = content2.length - tag.length;
1298
+ const options = chip?.resolve?.(tag.name);
1299
+ const $tag = createTag(
1300
+ tag.name,
1301
+ $root.ownerDocument,
1302
+ options?.color,
1303
+ options?.icon
1304
+ );
1290
1305
  const $space = createInlineWithText(data, $root.ownerDocument);
1291
1306
  if (tagStartPos === 0) {
1292
1307
  getParentTextBlock($inline).replaceChild($tag, $inline);
@@ -1317,10 +1332,10 @@ const insertText = ($root, data, range) => {
1317
1332
  range: createCollapsedRange(cursor)
1318
1333
  };
1319
1334
  };
1320
- const insertFromPaste = (data, range, visitor) => {
1335
+ const insertFromPaste = (data, range, visitor, parse) => {
1321
1336
  const cursor = removeContentInRange(range);
1322
1337
  const { $text, $inline, offset, isAfterInline } = cursor;
1323
- const $mdFragment = parseMarkdown(data, visitor);
1338
+ const $mdFragment = parseMarkdown(data, visitor, parse?.chipReg, parse?.escapeReg);
1324
1339
  if ($mdFragment.childNodes.length === 0) {
1325
1340
  return {
1326
1341
  prevent: true,
@@ -1511,7 +1526,7 @@ const serializeDescriptorReducer = (range) => (state, $n) => {
1511
1526
  if (isChip($n)) {
1512
1527
  const text2 = $n.text ?? "";
1513
1528
  if (text2.length > 0) {
1514
- state.push({ isTag: true, text: `{{${text2}}}` });
1529
+ state.push({ isTag: true, text: currentChipFormat(text2) });
1515
1530
  }
1516
1531
  return state;
1517
1532
  }
@@ -1735,13 +1750,26 @@ const serializeRoot = ($root, range) => {
1735
1750
  flushParagraphChunks();
1736
1751
  return chunks.join(MD_PARAGRAPH_JOIN);
1737
1752
  };
1738
- const serializeMarkdown = ($root, range) => {
1739
- if (range !== null) {
1740
- const aCursor = createIncomingCursorFromNodeWithOffset(range.startContainer, range.startOffset);
1741
- const bCursor = createIncomingCursorFromNodeWithOffset(range.endContainer, range.endOffset);
1742
- return serializeRoot($root, [aCursor, bCursor]);
1753
+ const defaultChipFormat = (name) => `{{${name}}}`;
1754
+ let currentChipFormat = defaultChipFormat;
1755
+ const serializeMarkdown = ($root, range, chipFormat) => {
1756
+ currentChipFormat = chipFormat ?? defaultChipFormat;
1757
+ try {
1758
+ if (range !== null) {
1759
+ const aCursor = createIncomingCursorFromNodeWithOffset(
1760
+ range.startContainer,
1761
+ range.startOffset
1762
+ );
1763
+ const bCursor = createIncomingCursorFromNodeWithOffset(
1764
+ range.endContainer,
1765
+ range.endOffset
1766
+ );
1767
+ return serializeRoot($root, [aCursor, bCursor]);
1768
+ }
1769
+ return serializeRoot($root, null);
1770
+ } finally {
1771
+ currentChipFormat = defaultChipFormat;
1743
1772
  }
1744
- return serializeRoot($root, null);
1745
1773
  };
1746
1774
  const createParseVisitor = (doc) => {
1747
1775
  let emojiBaseUrl = null;
package/utils/index.js CHANGED
@@ -7,7 +7,7 @@ import { getFirstFocusableElement, getFirstSlotElement, isElementFocused } from
7
7
  import { throttleAnimationFrame } from "./throttle.js";
8
8
  import { debounceAnimationFrame, debounceTimeout } from "./debounce.js";
9
9
  import { getReactEventHandler } from "./get-react-event-handler.js";
10
- import { isEmojiString, parseMarkdown } from "./markdown.js";
10
+ import { buildEscapedCharsReg, isEmojiString, parseMarkdown } from "./markdown.js";
11
11
  import { getTargetAttribute, getTargetByAttribute, getTargetIndexInParent, isTargetEqual } from "./event-target.js";
12
12
  import { isActivationKey } from "./control-keyboard-navigation.js";
13
13
  import { getUid } from "./uid.js";
@@ -19,6 +19,7 @@ export {
19
19
  attrValueToFloat,
20
20
  attrValueToInteger,
21
21
  attrValueToPixels,
22
+ buildEscapedCharsReg,
22
23
  clampNumber,
23
24
  cloneNode,
24
25
  composedContains,
@@ -20,5 +20,6 @@ export type TMarkdownParseVisitor = {
20
20
  listItem(): void;
21
21
  end(): Node;
22
22
  };
23
+ export declare const buildEscapedCharsReg: (extraChars: string) => RegExp;
23
24
  export declare const isEmojiString: (data: string) => boolean;
24
- export declare const parseMarkdown: (md: string, visitor: TMarkdownParseVisitor) => Node;
25
+ export declare const parseMarkdown: (md: string, visitor: TMarkdownParseVisitor, chipReg?: RegExp, escapeReg?: RegExp) => Node;
package/utils/markdown.js CHANGED
@@ -16,12 +16,19 @@ const regEmoji = new RegExp(`(?<emoji>\\p{RI}\\p{RI}|${regEmojiSeq}(?:\\u200D${r
16
16
  const regUList = /^(?<indent>[\t ]*?)[*+-][\t ]+(?<ultext>.*?)[\t ]*?$/;
17
17
  const regOList = /^(?<indent>[\t ]*?)\d+\.[\t ]+(?<oltext>.*?)[\t ]*?$/;
18
18
  const regEscapedChars = /\\(?<escaped>[\\\*_\[\]`~\{\}])/;
19
- const allRegs = [
20
- regEscapedChars,
19
+ const buildEscapedCharsReg = (extraChars) => {
20
+ if (extraChars === "") {
21
+ return regEscapedChars;
22
+ }
23
+ const classChars = Array.from(new Set(extraChars)).map((c) => c.replace(/[\\\]^-]/g, "\\$&")).join("");
24
+ return new RegExp(regEscapedChars.source.replace(/\]\)$/, `${classChars}])`));
25
+ };
26
+ const buildAllRegs = (chipReg, escapeReg = regEscapedChars) => [
27
+ escapeReg,
21
28
  regCodeTag,
22
29
  regButtonPlaceholder,
23
30
  regLink,
24
- regChip,
31
+ chipReg,
25
32
  regEm3Star,
26
33
  regEm2Star,
27
34
  regEm1Star,
@@ -34,12 +41,15 @@ const allRegs = [
34
41
  const isEmojiString = (data) => regEmoji.test(data);
35
42
  const excludeRegs = (regs, ...excluding) => regs.filter((r) => !excluding.includes(r));
36
43
  const matchClosest = (regs, line) => {
37
- return regs.map((reg) => reg.exec(line)).reduce((res, match) => {
38
- if (match === null) {
44
+ return regs.map((reg) => {
45
+ const match = reg.exec(line);
46
+ return match === null ? null : { reg, match };
47
+ }).reduce((res, current) => {
48
+ if (current === null) {
39
49
  return res;
40
50
  }
41
- if (res === null || res.index > match.index) {
42
- return match;
51
+ if (res === null || res.match.index > current.match.index) {
52
+ return current;
43
53
  }
44
54
  return res;
45
55
  }, null);
@@ -49,10 +59,11 @@ const INITIAL_CONTEXT = {
49
59
  isItalic: false,
50
60
  isStrikethrough: false
51
61
  };
52
- const createLineParser = (visitor) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
62
+ const createLineParser = (visitor, chipReg) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
53
63
  let line = md;
54
- let match = null;
55
- while ((match = matchClosest(regs, line)) !== null) {
64
+ let result = null;
65
+ while ((result = matchClosest(regs, line)) !== null) {
66
+ const { reg, match } = result;
56
67
  const groups = match.groups;
57
68
  const matchedStr = match[0];
58
69
  if (match.index > 0) {
@@ -76,11 +87,12 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
76
87
  if (groups?.code != null) {
77
88
  visitor.codetag(groups.code);
78
89
  }
79
- if (groups?.chip != null) {
80
- if (visitor.linkPlaceholder(groups.chip)) {
81
- continue;
90
+ if (reg === chipReg) {
91
+ const chipName = groups?.chip ?? match[1];
92
+ if (chipName != null && !visitor.linkPlaceholder(chipName)) {
93
+ visitor.tag(chipName);
82
94
  }
83
- visitor.tag(groups.chip);
95
+ continue;
84
96
  }
85
97
  if (groups?.emoji != null) {
86
98
  visitor.emoji(groups.emoji);
@@ -118,8 +130,9 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
118
130
  visitor.inline(line, context);
119
131
  }
120
132
  };
121
- const createBlockParser = (visitor) => {
122
- const parseLine = createLineParser(visitor);
133
+ const createBlockParser = (visitor, chipReg, escapeReg = regEscapedChars) => {
134
+ const parseLine = createLineParser(visitor, chipReg);
135
+ const allRegs = buildAllRegs(chipReg, escapeReg);
123
136
  const listRegs = [regUList, regOList];
124
137
  const getListItemText = (match) => {
125
138
  return match.groups.ultext ?? match.groups.oltext;
@@ -133,12 +146,12 @@ const createBlockParser = (visitor) => {
133
146
  const listMatch = matchClosest(listRegs, lines[i]);
134
147
  const isListLine = listMatch !== null;
135
148
  if (isListLine) {
136
- const groups = listMatch.groups;
149
+ const groups = listMatch.match.groups;
137
150
  const isOrderedList = groups.oltext != null;
138
151
  mode = isOrderedList ? BLOCK_MODE_OLIST : BLOCK_MODE_ULIST;
139
152
  visitor.list(isOrderedList);
140
153
  const listItemInitialIndent = groups.indent;
141
- let listItemLines = [getListItemText(listMatch)];
154
+ let listItemLines = [getListItemText(listMatch.match)];
142
155
  visitor.listItem();
143
156
  for (++i; i < lines.length; ++i) {
144
157
  const listItemMatch = isOrderedList ? regOList.exec(lines[i]) : regUList.exec(lines[i]);
@@ -166,14 +179,15 @@ const createBlockParser = (visitor) => {
166
179
  }
167
180
  };
168
181
  };
169
- const parseMarkdown = (md, visitor) => {
170
- const parseBlock = createBlockParser(visitor);
182
+ const parseMarkdown = (md, visitor, chipReg = regChip, escapeReg = regEscapedChars) => {
183
+ const parseBlock = createBlockParser(visitor, chipReg, escapeReg);
171
184
  for (const block of md.trim().split(regParagraph)) {
172
185
  parseBlock(block.trim().split(regLinebreak));
173
186
  }
174
187
  return visitor.end();
175
188
  };
176
189
  export {
190
+ buildEscapedCharsReg,
177
191
  isEmojiString,
178
192
  parseMarkdown
179
193
  };