@limetech/lime-elements 37.39.1 → 37.40.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.
Files changed (21) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/cjs/limel-prosemirror-adapter.cjs.entry.js +127 -81
  3. package/dist/cjs/limel-prosemirror-adapter.cjs.entry.js.map +1 -1
  4. package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-commands.js +70 -65
  5. package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-commands.js.map +1 -1
  6. package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-items.js +9 -8
  7. package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-items.js.map +1 -1
  8. package/dist/collection/components/text-editor/prosemirror-adapter/menu/types.js +32 -0
  9. package/dist/collection/components/text-editor/prosemirror-adapter/menu/types.js.map +1 -0
  10. package/dist/collection/components/text-editor/prosemirror-adapter/prosemirror-adapter.js +19 -8
  11. package/dist/collection/components/text-editor/prosemirror-adapter/prosemirror-adapter.js.map +1 -1
  12. package/dist/esm/limel-prosemirror-adapter.entry.js +127 -81
  13. package/dist/esm/limel-prosemirror-adapter.entry.js.map +1 -1
  14. package/dist/lime-elements/lime-elements.esm.js +1 -1
  15. package/dist/lime-elements/{p-fecf1bf1.entry.js → p-e9f943ac.entry.js} +2 -2
  16. package/dist/lime-elements/p-e9f943ac.entry.js.map +1 -0
  17. package/dist/types/components/text-editor/prosemirror-adapter/menu/menu-commands.d.ts +10 -6
  18. package/dist/types/components/text-editor/prosemirror-adapter/menu/menu-items.d.ts +2 -1
  19. package/dist/types/components/text-editor/prosemirror-adapter/prosemirror-adapter.d.ts +2 -0
  20. package/package.json +1 -1
  21. package/dist/lime-elements/p-fecf1bf1.entry.js.map +0 -1
@@ -15907,78 +15907,114 @@ function exampleSetup(options) {
15907
15907
  }));
15908
15908
  }
15909
15909
 
15910
- class MenuCommandFactory {
15911
- constructor(schema) {
15912
- this.markNames = {
15913
- bold: 'strong',
15914
- italic: 'em',
15915
- underline: 'underline',
15916
- blockquote: 'blockquote',
15917
- headerlevel1: 'headerlevel1',
15918
- headerlevel2: 'headerlevel2',
15919
- headerlevel3: 'headerlevel3',
15920
- addorremovelink: 'link',
15921
- numberedlist: 'ordered_list',
15922
- list: 'bullet_list',
15910
+ /**
15911
+ * The `EditorMenuType` type is used to specify the type of menu items that can be added to the editor toolbar.
15912
+ * Each one represents a different type to be used for creating the prosemirror commands relevant to the button.
15913
+ * The values correspond to the types that can be used with the `prosemirror-commands` library.
15914
+ * @beta
15915
+ */
15916
+ const EditorMenuTypes = {
15917
+ Bold: 'strong',
15918
+ Italic: 'em',
15919
+ Blockquote: 'blockquote',
15920
+ HeaderLevel1: 'headerlevel1',
15921
+ HeaderLevel2: 'headerlevel2',
15922
+ HeaderLevel3: 'headerlevel3',
15923
+ Link: 'link',
15924
+ OrderedList: 'ordered_list',
15925
+ BulletList: 'bullet_list',
15926
+ };
15927
+ /**
15928
+ * `LevelMapping` is used to map string identifiers to numerical header levels.
15929
+ * It provides a way to represent different levels of headings in ProseMirror commands.
15930
+ *
15931
+ * The `Heading` identifier is not a valid level and is used to identify the node type.
15932
+ * The numerical values are used for creating ProseMirror commands to set the level of a heading node in the editor.
15933
+ * @beta
15934
+ */
15935
+ const LevelMapping = {
15936
+ Heading: 'heading',
15937
+ one: 1,
15938
+ two: 2,
15939
+ three: 3,
15940
+ };
15941
+
15942
+ const createToggleMarkCommand = (schema, markName, url) => {
15943
+ const markType = schema.marks[markName];
15944
+ if (!markType) {
15945
+ throw new Error(`Mark "${markName}" not found in schema`);
15946
+ }
15947
+ if (markName === EditorMenuTypes.Link && url) {
15948
+ const attrs = {
15949
+ href: url,
15950
+ target: isExternalLink(url) ? '_blank' : null,
15923
15951
  };
15924
- this.schema = schema;
15952
+ return toggleMark(markType, attrs);
15925
15953
  }
15926
- createCommand(mark) {
15927
- if (this.markNames[mark]) {
15928
- mark = this.markNames[mark];
15929
- }
15930
- switch (mark) {
15931
- case 'strong':
15932
- case 'em':
15933
- case 'underline':
15934
- return this.createToggleMarkCommand(mark);
15935
- case 'paragraph':
15936
- return this.createSetNodeTypeCommand(mark);
15937
- case 'headerlevel1':
15938
- case 'headerlevel2':
15939
- case 'headerlevel3':
15940
- return this.createSetNodeTypeCommand('heading', parseInt(mark[mark.length - 1], 10));
15941
- case 'blockquote':
15942
- return this.createWrapInCommand(mark);
15943
- case 'ordered_list':
15944
- case 'bullet_list':
15945
- return this.createListCommand(mark);
15946
- default:
15947
- throw new Error(`The Mark "${mark}" is not supported`);
15948
- }
15954
+ return toggleMark(markType);
15955
+ };
15956
+ const isExternalLink = (url) => {
15957
+ return !url.startsWith(window.location.origin);
15958
+ };
15959
+ const createSetNodeTypeCommand = (schema, nodeType, level) => {
15960
+ const type = schema.nodes[nodeType];
15961
+ if (!type) {
15962
+ throw new Error(`Node type "${nodeType}" not found in schema`);
15949
15963
  }
15950
- createToggleMarkCommand(markName) {
15951
- const markType = this.schema.marks[markName];
15952
- if (!markType) {
15953
- throw new Error(`Mark "${markName}" not found in schema`);
15954
- }
15955
- return toggleMark(markType);
15964
+ if (nodeType === 'heading' && level) {
15965
+ return setBlockType(type, { level: level });
15956
15966
  }
15957
- createSetNodeTypeCommand(nodeType, level) {
15958
- const type = this.schema.nodes[nodeType];
15959
- if (!type) {
15960
- throw new Error(`Node type "${nodeType}" not found in schema`);
15961
- }
15962
- if (nodeType === 'heading' && level) {
15963
- return setBlockType(type, { level: level });
15964
- }
15965
- else {
15966
- return setBlockType(type);
15967
- }
15967
+ else {
15968
+ return setBlockType(type);
15968
15969
  }
15969
- createWrapInCommand(nodeType) {
15970
- const type = this.schema.nodes[nodeType];
15971
- if (!type) {
15972
- throw new Error(`Node type "${nodeType}" not found in schema`);
15973
- }
15974
- return wrapIn(type);
15970
+ };
15971
+ const createWrapInCommand = (schema, nodeType) => {
15972
+ const type = schema.nodes[nodeType];
15973
+ if (!type) {
15974
+ throw new Error(`Node type "${nodeType}" not found in schema`);
15975
15975
  }
15976
- createListCommand(listType) {
15977
- const type = this.schema.nodes[listType];
15978
- if (!type) {
15979
- throw new Error(`List type "${listType}" not found in schema`);
15976
+ return wrapIn(type);
15977
+ };
15978
+ const createListCommand = (schema, listType) => {
15979
+ const type = schema.nodes[listType];
15980
+ if (!type) {
15981
+ throw new Error(`List type "${listType}" not found in schema`);
15982
+ }
15983
+ return wrapInList(type);
15984
+ };
15985
+ const commandMapping = {
15986
+ strong: createToggleMarkCommand,
15987
+ em: createToggleMarkCommand,
15988
+ underline: createToggleMarkCommand,
15989
+ headerlevel1: (schema) => createSetNodeTypeCommand(schema, LevelMapping.Heading, LevelMapping.one),
15990
+ headerlevel2: (schema) => createSetNodeTypeCommand(schema, LevelMapping.Heading, LevelMapping.two),
15991
+ headerlevel3: (schema) => createSetNodeTypeCommand(schema, LevelMapping.Heading, LevelMapping.three),
15992
+ blockquote: createWrapInCommand,
15993
+ /* eslint-disable camelcase */
15994
+ ordered_list: createListCommand,
15995
+ bullet_list: createListCommand,
15996
+ /* eslint-enable camelcase */
15997
+ link: createToggleMarkCommand,
15998
+ };
15999
+ class MenuCommandFactory {
16000
+ constructor(schema) {
16001
+ this.schema = schema;
16002
+ }
16003
+ getCommand(mark, url) {
16004
+ const commandFunc = commandMapping[mark];
16005
+ if (!commandFunc) {
16006
+ throw new Error(`The Mark "${mark}" is not supported`);
15980
16007
  }
15981
- return wrapInList(type);
16008
+ return commandFunc(this.schema, mark, url);
16009
+ }
16010
+ buildKeymap() {
16011
+ return {
16012
+ 'Mod-B': this.getCommand(EditorMenuTypes.Bold),
16013
+ 'Mod-I': this.getCommand(EditorMenuTypes.Italic),
16014
+ 'Mod-Shift-1': this.getCommand(EditorMenuTypes.HeaderLevel1),
16015
+ 'Mod-Shift-2': this.getCommand(EditorMenuTypes.HeaderLevel2),
16016
+ 'Mod-Shift-3': this.getCommand(EditorMenuTypes.HeaderLevel3),
16017
+ };
15982
16018
  }
15983
16019
  }
15984
16020
 
@@ -15992,14 +16028,14 @@ const getCommandSymbols = () => {
15992
16028
  const { mod, shift } = getCommandSymbols();
15993
16029
  const textEditorMenuItems = [
15994
16030
  {
15995
- value: 'strong',
16031
+ value: EditorMenuTypes.Bold,
15996
16032
  text: 'Bold',
15997
16033
  commandText: `${mod} B`,
15998
16034
  icon: '-lime-text-bold',
15999
16035
  iconOnly: true,
16000
16036
  },
16001
16037
  {
16002
- value: 'em',
16038
+ value: EditorMenuTypes.Italic,
16003
16039
  text: 'Italic',
16004
16040
  commandText: `${mod} I`,
16005
16041
  icon: '-lime-text-italic',
@@ -16007,21 +16043,21 @@ const textEditorMenuItems = [
16007
16043
  },
16008
16044
  { separator: true },
16009
16045
  {
16010
- value: 'headerlevel1',
16046
+ value: EditorMenuTypes.HeaderLevel1,
16011
16047
  text: 'Header Level 1',
16012
16048
  commandText: `${mod} ${shift} 1`,
16013
16049
  icon: '-lime-text-h-heading-1',
16014
16050
  iconOnly: true,
16015
16051
  },
16016
16052
  {
16017
- value: 'headerlevel2',
16053
+ value: EditorMenuTypes.HeaderLevel2,
16018
16054
  text: 'Header Level 2',
16019
16055
  commandText: `${mod} ${shift} 2`,
16020
16056
  icon: '-lime-text-h-heading-2',
16021
16057
  iconOnly: true,
16022
16058
  },
16023
16059
  {
16024
- value: 'headerlevel3',
16060
+ value: EditorMenuTypes.HeaderLevel3,
16025
16061
  text: 'Header Level 3',
16026
16062
  commandText: `${mod} ${shift} 3`,
16027
16063
  icon: '-lime-text-h-heading-3',
@@ -16029,19 +16065,19 @@ const textEditorMenuItems = [
16029
16065
  },
16030
16066
  { separator: true },
16031
16067
  {
16032
- value: 'bullet_list',
16068
+ value: EditorMenuTypes.BulletList,
16033
16069
  text: 'Bullet list',
16034
16070
  icon: '-lime-text-bulleted-list',
16035
16071
  iconOnly: true,
16036
16072
  },
16037
16073
  {
16038
- value: 'ordered_list',
16074
+ value: EditorMenuTypes.OrderedList,
16039
16075
  text: 'Numbered list',
16040
16076
  icon: '-lime-text-ordered-list',
16041
16077
  iconOnly: true,
16042
16078
  },
16043
16079
  {
16044
- value: 'blockquote',
16080
+ value: EditorMenuTypes.Blockquote,
16045
16081
  text: 'Blockquote',
16046
16082
  icon: '-lime-text-blockquote',
16047
16083
  iconOnly: true,
@@ -24714,6 +24750,7 @@ const ProsemirrorAdapter = class {
24714
24750
  constructor(hostRef) {
24715
24751
  registerInstance(this, hostRef);
24716
24752
  this.change = createEvent(this, "change", 7);
24753
+ this.editorKeyMap = {};
24717
24754
  this.initializeTextEditor = async () => {
24718
24755
  this.actionBarItems = textEditorMenuItems;
24719
24756
  const mySchema = new Schema({
@@ -24728,13 +24765,19 @@ const ProsemirrorAdapter = class {
24728
24765
  await this.contentConverter.parseAsHTML(this.value, schema$1);
24729
24766
  }
24730
24767
  const initialDoc = DOMParser.fromSchema(mySchema).parse(initialContentElement);
24768
+ this.menuCommandFactory = new MenuCommandFactory(mySchema);
24769
+ this.editorKeyMap = this.menuCommandFactory.buildKeymap();
24770
+ const keymapPlugin = keymap(this.editorKeyMap);
24731
24771
  this.view = new EditorView(this.host.shadowRoot.querySelector('#editor'), {
24732
24772
  state: EditorState.create({
24733
24773
  doc: initialDoc,
24734
- plugins: exampleSetup({
24735
- schema: mySchema,
24736
- menuBar: false,
24737
- }),
24774
+ plugins: [
24775
+ ...exampleSetup({
24776
+ schema: mySchema,
24777
+ menuBar: false,
24778
+ }),
24779
+ keymapPlugin,
24780
+ ],
24738
24781
  }),
24739
24782
  dispatchTransaction: (transaction) => {
24740
24783
  const newState = this.view.state.apply(transaction);
@@ -24749,10 +24792,9 @@ const ProsemirrorAdapter = class {
24749
24792
  };
24750
24793
  this.handleActionBarItem = (event) => {
24751
24794
  event.preventDefault();
24752
- const { text } = event.detail;
24753
- const mark = text.replace(/\s/g, '').toLowerCase();
24795
+ const { value } = event.detail;
24754
24796
  try {
24755
- const command = this.menuCommandFactory.createCommand(mark);
24797
+ const command = this.menuCommandFactory.getCommand(value);
24756
24798
  this.executeCommand(command);
24757
24799
  }
24758
24800
  catch (error) {
@@ -24812,7 +24854,11 @@ const ProsemirrorAdapter = class {
24812
24854
  transaction = tr;
24813
24855
  });
24814
24856
  this.view.dispatch(transaction);
24815
- this.view.focus();
24857
+ this.setFocus();
24858
+ }
24859
+ setFocus() {
24860
+ var _a;
24861
+ (_a = this.view) === null || _a === void 0 ? void 0 : _a.focus();
24816
24862
  }
24817
24863
  get host() { return getElement(this); }
24818
24864
  static get watchers() { return {