lakelib 0.3.6 → 0.3.8

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/lib/lake.d.ts CHANGED
@@ -3,6 +3,12 @@ import { LocalizedString } from 'typesafe-i18n';
3
3
 
4
4
  type KeyValue = Record<string, string>;
5
5
 
6
+ type ContentStyleValue = string | string[] | RegExp;
7
+ type ContentStyle = Record<string, ContentStyleValue>;
8
+ type ContentAttributeValue = string | string[] | RegExp | ContentStyle;
9
+ type ContentAttribute = Record<string, ContentAttributeValue>;
10
+ type ContentRules = Record<string, string | ContentAttribute>;
11
+
6
12
  interface EventItem {
7
13
  type: string;
8
14
  listener: EventListener;
@@ -1451,13 +1457,17 @@ declare class History {
1451
1457
  */
1452
1458
  readonly list: Nodes[];
1453
1459
  /**
1454
- * An index that always indicates the position at which new content is stored.
1460
+ * A number that always indicates the position at which new content is stored.
1455
1461
  */
1456
1462
  index: number;
1457
1463
  /**
1458
1464
  * The maximum length of the history. Once this limit is reached, the earliest item in the list will be removed.
1459
1465
  */
1460
1466
  limit: number;
1467
+ /**
1468
+ * A ContentRules object defining the HTML parsing rules used by HTMLParser.
1469
+ */
1470
+ contentRules: ContentRules;
1461
1471
  /**
1462
1472
  * An EventEmitter object used to set up events.
1463
1473
  */
@@ -1674,24 +1684,15 @@ interface Config {
1674
1684
  placeholder: string;
1675
1685
  indentWithTab: boolean;
1676
1686
  lang: string;
1687
+ contentRules: ContentRules;
1677
1688
  minChangeSize: number;
1678
1689
  historySize: number;
1679
1690
  onMessage: OnMessage;
1680
1691
  [name: string]: any;
1681
1692
  }
1682
- interface EditorConfig {
1693
+ interface EditorConfig extends Partial<Config> {
1683
1694
  root: string | Node | Nodes;
1684
1695
  toolbar?: Toolbar;
1685
- value?: string;
1686
- readonly?: boolean;
1687
- spellcheck?: boolean;
1688
- tabIndex?: number;
1689
- placeholder?: string;
1690
- indentWithTab?: boolean;
1691
- lang?: string;
1692
- minChangeSize?: number;
1693
- onMessage?: OnMessage;
1694
- [name: string]: any;
1695
1696
  }
1696
1697
  /**
1697
1698
  * The Editor interface provides properties and methods for rendering and manipulating the editor.
@@ -1993,6 +1994,8 @@ type SlashItem = SlashButtonItem | SlashUploadItem;
1993
1994
 
1994
1995
  declare const icons: Map<string, string>;
1995
1996
 
1997
+ declare function getContentRules(): ContentRules;
1998
+
1996
1999
  /**
1997
2000
  * Returns a Nodes object representing a collection of the nodes.
1998
2001
  * This function is similar to jQuery, but its implementation is very simple.
@@ -2024,7 +2027,7 @@ declare function getBox(boxNode: string | Node | Nodes): Box;
2024
2027
  declare class HTMLParser {
2025
2028
  private readonly rules;
2026
2029
  private readonly source;
2027
- constructor(content: string | Nodes, rules?: any);
2030
+ constructor(content: string | Nodes, rules?: ContentRules);
2028
2031
  /**
2029
2032
  * Parses the given HTML string and returns the body element from the result.
2030
2033
  */
@@ -2149,4 +2152,4 @@ declare class Dropdown {
2149
2152
  unmount(): void;
2150
2153
  }
2151
2154
 
2152
- export { type ActiveItem, Box, type BoxComponent, type BoxValue, Button, type CommandItem, Dropdown, type DropdownItem, type DropdownMenuItem, Editor, Fragment, HTMLParser, type InitializePlugin, type KeyValue, type MentionItem, type NodePath, Nodes, Range$1 as Range, type SelectionState, type SlashButtonItem, type SlashItem, type SlashUploadItem, TextParser, Toolbar, type ToolbarButtonItem, type ToolbarDropdownItem, type ToolbarItem, type ToolbarUploadItem, type UnmountPlugin, addMark, deleteContents, getBox, icons, insertBlock, insertBookmark, insertBox, insertContents, query, removeBox, removeMark, setBlocks, splitBlock, splitMarks, template, toBookmark, toHex };
2155
+ export { type ActiveItem, Box, type BoxComponent, type BoxValue, Button, type CommandItem, type ContentRules, Dropdown, type DropdownItem, type DropdownMenuItem, Editor, Fragment, HTMLParser, type InitializePlugin, type KeyValue, type MentionItem, type NodePath, Nodes, Range$1 as Range, type SelectionState, type SlashButtonItem, type SlashItem, type SlashUploadItem, TextParser, Toolbar, type ToolbarButtonItem, type ToolbarDropdownItem, type ToolbarItem, type ToolbarUploadItem, type UnmountPlugin, addMark, deleteContents, getBox, getContentRules, icons, insertBlock, insertBookmark, insertBox, insertContents, query, removeBox, removeMark, setBlocks, splitBlock, splitMarks, template, toBookmark, toHex };
package/lib/lake.js CHANGED
@@ -285,6 +285,112 @@ const icons = new Map([
285
285
  ['danger', danger],
286
286
  ]);
287
287
 
288
+ const blockAttributeRules = {
289
+ id: /^[\w-]+$/,
290
+ class: /^[\w\- ]+$/,
291
+ style: {
292
+ 'text-align': ['left', 'center', 'right', 'justify', 'start', 'end'],
293
+ 'margin-left': /^-?\d+px$/i,
294
+ 'text-indent': /^-?\d+em$/i,
295
+ },
296
+ };
297
+ function getContentRules() {
298
+ return {
299
+ h1: Object.assign({}, blockAttributeRules),
300
+ h2: Object.assign({}, blockAttributeRules),
301
+ h3: Object.assign({}, blockAttributeRules),
302
+ h4: Object.assign({}, blockAttributeRules),
303
+ h5: Object.assign({}, blockAttributeRules),
304
+ h6: Object.assign({}, blockAttributeRules),
305
+ p: Object.assign({}, blockAttributeRules),
306
+ blockquote: Object.assign(Object.assign({}, blockAttributeRules), { type: ['info', 'tip', 'success', 'warning', 'error', 'danger'] }),
307
+ ul: Object.assign(Object.assign({}, blockAttributeRules), { type: 'checklist', indent: /^\d+$/ }),
308
+ ol: Object.assign(Object.assign({}, blockAttributeRules), { start: /^\d+$/, indent: /^\d+$/ }),
309
+ li: {
310
+ value: ['true', 'false'],
311
+ },
312
+ table: {
313
+ style: {
314
+ width: /^-?\d+(px|%)$/i,
315
+ height: /^-?\d+(px|%)$/i,
316
+ border: /^[^"]+$/,
317
+ 'border-width': /^-?\d+px$/i,
318
+ 'border-style': /^[\w-]+$/,
319
+ 'border-color': /^[^"]+$/,
320
+ 'background-color': /^[^"]+$/,
321
+ },
322
+ },
323
+ tr: {
324
+ style: {
325
+ height: /^-?\d+(px|%)$/i,
326
+ },
327
+ },
328
+ th: 'td',
329
+ td: {
330
+ colspan: /^\d+$/,
331
+ rowspan: /^\d+$/,
332
+ style: {
333
+ width: /^-?\d+(px|%)$/i,
334
+ height: /^-?\d+(px|%)$/i,
335
+ border: /^[^"]+$/,
336
+ 'border-width': /^-?\d+px$/i,
337
+ 'border-style': /^[\w-]+$/,
338
+ 'border-color': /^[^"]+$/,
339
+ 'background-color': /^[^"]+$/,
340
+ 'text-align': ['left', 'center', 'right', 'justify'],
341
+ },
342
+ },
343
+ 'lake-box': {
344
+ type: ['inline', 'block'],
345
+ name: /^[\w-]+$/,
346
+ value: /^[^"]+$/,
347
+ focus: ['start', 'center', 'end'],
348
+ },
349
+ br: {},
350
+ hr: {},
351
+ img: {
352
+ src: /^[^"]+$/,
353
+ width: /^-?\d+px$/i,
354
+ height: /^-?\d+px$/i,
355
+ 'data-lake-value': /^[^"]+$/,
356
+ alt: /^[^"]+$/,
357
+ style: {
358
+ width: /^-?\d+px$/i,
359
+ height: /^-?\d+px$/i,
360
+ },
361
+ },
362
+ span: {
363
+ class: /^[\w\- ]+$/,
364
+ style: {
365
+ color: /^[^"]+$/,
366
+ 'background-color': /^[^"]+$/,
367
+ 'font-family': /^[^;]+$/,
368
+ 'font-size': /^[^"]+$/,
369
+ },
370
+ },
371
+ b: 'strong',
372
+ strong: {},
373
+ em: 'i',
374
+ i: {},
375
+ u: {},
376
+ s: {},
377
+ sub: {},
378
+ sup: {},
379
+ code: {},
380
+ a: {
381
+ class: /^[\w\- ]+$/,
382
+ name: /^[\w-]+$/,
383
+ href: /^[^"]+$/,
384
+ target: /^[\w-]+$/,
385
+ rel: /^[^"]+$/,
386
+ download: /^[^"]+$/,
387
+ },
388
+ 'lake-bookmark': {
389
+ type: ['anchor', 'focus'],
390
+ },
391
+ };
392
+ }
393
+
288
394
  // Returns an array representing a list of the document's elements.
289
395
  function toNodeList(content, valueType) {
290
396
  const nodeList = [];
@@ -4395,7 +4501,7 @@ class Toolbar {
4395
4501
  for (const item of this.dropdownItemList) {
4396
4502
  let selectedValues = selectedValuesMap.get(item.name);
4397
4503
  if (selectedValues === undefined) {
4398
- selectedValues = item.selectedValues ? item.selectedValues(activeItems) : [];
4504
+ selectedValues = item.selectedValues && activeItems.length > 0 ? item.selectedValues(activeItems) : [];
4399
4505
  }
4400
4506
  const dropdownNode = this.container.find(`div.lake-dropdown[name="${item.name}"]`);
4401
4507
  let isDisabled = disabledNameMap.get(item.name);
@@ -4843,112 +4949,6 @@ class Fragment {
4843
4949
  }
4844
4950
  }
4845
4951
 
4846
- const blockAttributeRules = {
4847
- id: /^[\w-]+$/,
4848
- class: /^[\w\- ]+$/,
4849
- style: {
4850
- 'text-align': ['left', 'center', 'right', 'justify', 'start', 'end'],
4851
- 'margin-left': /^-?\d+px$/i,
4852
- 'text-indent': /^-?\d+em$/i,
4853
- },
4854
- };
4855
- function getElementRules() {
4856
- return {
4857
- h1: Object.assign({}, blockAttributeRules),
4858
- h2: Object.assign({}, blockAttributeRules),
4859
- h3: Object.assign({}, blockAttributeRules),
4860
- h4: Object.assign({}, blockAttributeRules),
4861
- h5: Object.assign({}, blockAttributeRules),
4862
- h6: Object.assign({}, blockAttributeRules),
4863
- p: Object.assign({}, blockAttributeRules),
4864
- blockquote: Object.assign(Object.assign({}, blockAttributeRules), { type: ['info', 'tip', 'success', 'warning', 'error', 'danger'] }),
4865
- ul: Object.assign(Object.assign({}, blockAttributeRules), { type: 'checklist', indent: /^\d+$/ }),
4866
- ol: Object.assign(Object.assign({}, blockAttributeRules), { start: /^\d+$/, indent: /^\d+$/ }),
4867
- li: {
4868
- value: ['true', 'false'],
4869
- },
4870
- table: {
4871
- style: {
4872
- width: /^-?\d+(px|%)$/i,
4873
- height: /^-?\d+(px|%)$/i,
4874
- border: /^[^"]+$/,
4875
- 'border-width': /^-?\d+px$/i,
4876
- 'border-style': /^[\w-]+$/,
4877
- 'border-color': /^[^"]+$/,
4878
- 'background-color': /^[^"]+$/,
4879
- },
4880
- },
4881
- tr: {
4882
- style: {
4883
- height: /^-?\d+(px|%)$/i,
4884
- },
4885
- },
4886
- th: 'td',
4887
- td: {
4888
- colspan: /^\d+$/,
4889
- rowspan: /^\d+$/,
4890
- style: {
4891
- width: /^-?\d+(px|%)$/i,
4892
- height: /^-?\d+(px|%)$/i,
4893
- border: /^[^"]+$/,
4894
- 'border-width': /^-?\d+px$/i,
4895
- 'border-style': /^[\w-]+$/,
4896
- 'border-color': /^[^"]+$/,
4897
- 'background-color': /^[^"]+$/,
4898
- 'text-align': ['left', 'center', 'right', 'justify'],
4899
- },
4900
- },
4901
- 'lake-box': {
4902
- type: ['inline', 'block'],
4903
- name: /^[\w-]+$/,
4904
- value: /^[^"]+$/,
4905
- focus: ['start', 'center', 'end'],
4906
- },
4907
- br: {},
4908
- hr: {},
4909
- img: {
4910
- src: /^[^"]+$/,
4911
- width: /^-?\d+px$/i,
4912
- height: /^-?\d+px$/i,
4913
- 'data-lake-value': /^[^"]+$/,
4914
- alt: /^[^"]+$/,
4915
- style: {
4916
- width: /^-?\d+px$/i,
4917
- height: /^-?\d+px$/i,
4918
- },
4919
- },
4920
- span: {
4921
- class: /^[\w\- ]+$/,
4922
- style: {
4923
- color: /^[^"]+$/,
4924
- 'background-color': /^[^"]+$/,
4925
- 'font-family': /^[^;]+$/,
4926
- 'font-size': /^[^"]+$/,
4927
- },
4928
- },
4929
- b: 'strong',
4930
- strong: {},
4931
- em: 'i',
4932
- i: {},
4933
- u: {},
4934
- s: {},
4935
- sub: {},
4936
- sup: {},
4937
- code: {},
4938
- a: {
4939
- class: /^[\w\- ]+$/,
4940
- name: /^[\w-]+$/,
4941
- href: /^[^"]+$/,
4942
- target: /^[\w-]+$/,
4943
- rel: /^[^"]+$/,
4944
- download: /^[^"]+$/,
4945
- },
4946
- 'lake-bookmark': {
4947
- type: ['anchor', 'focus'],
4948
- },
4949
- };
4950
- }
4951
-
4952
4952
  // Converts a string from style property of element to a key-value object that contains a list of all styles properties.
4953
4953
  function parseStyle(styleValue) {
4954
4954
  styleValue = styleValue.replace(/&quot;/gi, '"');
@@ -4967,7 +4967,7 @@ function parseStyle(styleValue) {
4967
4967
  * The HTMLParser interface provides the ability to parse an HTML string according to specified rules.
4968
4968
  */
4969
4969
  class HTMLParser {
4970
- constructor(content, rules = getElementRules()) {
4970
+ constructor(content, rules = getContentRules()) {
4971
4971
  this.rules = rules;
4972
4972
  if (typeof content === 'string') {
4973
4973
  this.source = this.parseHTML(content);
@@ -6273,7 +6273,7 @@ function removeBox(range) {
6273
6273
  return box;
6274
6274
  }
6275
6275
 
6276
- var version = "0.3.6";
6276
+ var version = "0.3.8";
6277
6277
 
6278
6278
  // Converts the custom HTML tags to the special tags that can not be parsed by browser.
6279
6279
  function denormalizeValue(value) {
@@ -7212,13 +7212,17 @@ class History {
7212
7212
  */
7213
7213
  this.list = [];
7214
7214
  /**
7215
- * An index that always indicates the position at which new content is stored.
7215
+ * A number that always indicates the position at which new content is stored.
7216
7216
  */
7217
7217
  this.index = 0;
7218
7218
  /**
7219
7219
  * The maximum length of the history. Once this limit is reached, the earliest item in the list will be removed.
7220
7220
  */
7221
7221
  this.limit = 100;
7222
+ /**
7223
+ * A ContentRules object defining the HTML parsing rules used by HTMLParser.
7224
+ */
7225
+ this.contentRules = getContentRules();
7222
7226
  /**
7223
7227
  * An EventEmitter object used to set up events.
7224
7228
  */
@@ -7232,7 +7236,7 @@ class History {
7232
7236
  .replace(/<lake-bookmark\s+type="focus">\s*<\/lake-bookmark>/gi, '');
7233
7237
  }
7234
7238
  getValue(container) {
7235
- return new HTMLParser(container).getHTML();
7239
+ return new HTMLParser(container, this.contentRules).getHTML();
7236
7240
  }
7237
7241
  addIdToBoxes(node) {
7238
7242
  node.find('lake-box').each(nativeNode => {
@@ -7575,6 +7579,7 @@ const defaultConfig = {
7575
7579
  placeholder: '',
7576
7580
  indentWithTab: true,
7577
7581
  lang: 'en-US',
7582
+ contentRules: getContentRules(),
7578
7583
  minChangeSize: 5,
7579
7584
  historySize: 100,
7580
7585
  onMessage: (type, message) => {
@@ -7762,6 +7767,7 @@ class Editor {
7762
7767
  this.command = new Command(this.selection);
7763
7768
  this.history = new History(this.selection);
7764
7769
  this.history.limit = this.config.historySize;
7770
+ this.history.contentRules = this.config.contentRules;
7765
7771
  this.keystroke = new Keystroke(this.container);
7766
7772
  editors.set(this.container.id, this);
7767
7773
  }
@@ -8104,7 +8110,7 @@ class Editor {
8104
8110
  */
8105
8111
  setValue(value) {
8106
8112
  value = normalizeValue(value);
8107
- const htmlParser = new HTMLParser(value);
8113
+ const htmlParser = new HTMLParser(value, this.config.contentRules);
8108
8114
  const fragment = htmlParser.getFragment();
8109
8115
  this.container.empty();
8110
8116
  this.togglePlaceholderClass(htmlParser.getHTML());
@@ -8117,7 +8123,7 @@ class Editor {
8117
8123
  */
8118
8124
  getValue() {
8119
8125
  const item = this.history.cloneContainer();
8120
- let value = new HTMLParser(item).getHTML();
8126
+ let value = new HTMLParser(item, this.config.contentRules).getHTML();
8121
8127
  value = denormalizeValue(value);
8122
8128
  return value;
8123
8129
  }
@@ -8126,7 +8132,7 @@ class Editor {
8126
8132
  */
8127
8133
  render() {
8128
8134
  const value = normalizeValue(this.config.value);
8129
- const htmlParser = new HTMLParser(value);
8135
+ const htmlParser = new HTMLParser(value, this.config.contentRules);
8130
8136
  const fragment = htmlParser.getFragment();
8131
8137
  this.root.empty();
8132
8138
  this.root.append(this.containerWrapper);
@@ -8292,13 +8298,14 @@ function changeTagName(element, newTagName) {
8292
8298
  }
8293
8299
 
8294
8300
  const blockSelector = Array.from(blockTagNames).join(',');
8295
- function getPasteElementRules() {
8296
- const rules = getElementRules();
8301
+ function changeContentRules(rules) {
8297
8302
  rules.div = rules.p;
8298
8303
  for (const key of Object.keys(rules)) {
8299
8304
  const attributeRules = rules[key];
8300
- delete attributeRules.id;
8301
- delete attributeRules.class;
8305
+ if (typeof attributeRules !== 'string') {
8306
+ delete attributeRules.id;
8307
+ delete attributeRules.class;
8308
+ }
8302
8309
  }
8303
8310
  return rules;
8304
8311
  }
@@ -8496,7 +8503,7 @@ var paste = (editor) => {
8496
8503
  return;
8497
8504
  }
8498
8505
  const content = normalizeValue(dataTransfer.getData('text/html'));
8499
- const rules = getPasteElementRules();
8506
+ const rules = changeContentRules(editor.config.contentRules);
8500
8507
  const htmlParser = new HTMLParser(content, rules);
8501
8508
  const fragment = htmlParser.getFragment();
8502
8509
  editor.event.emit('beforepaste', fragment);
@@ -13765,5 +13772,5 @@ Editor.plugin.add('arrowKeys', arrowKeys);
13765
13772
  Editor.plugin.add('escapeKey', escapeKey);
13766
13773
  Editor.plugin.add('slash', slash);
13767
13774
 
13768
- export { Box, Button, Dropdown, Editor, Fragment, HTMLParser, Nodes, Range, TextParser, Toolbar, addMark, deleteContents, getBox, icons, insertBlock, insertBookmark, insertBox, insertContents, query, removeBox, removeMark, setBlocks, splitBlock$1 as splitBlock, splitMarks, template, toBookmark, toHex };
13775
+ export { Box, Button, Dropdown, Editor, Fragment, HTMLParser, Nodes, Range, TextParser, Toolbar, addMark, deleteContents, getBox, getContentRules, icons, insertBlock, insertBookmark, insertBox, insertContents, query, removeBox, removeMark, setBlocks, splitBlock$1 as splitBlock, splitMarks, template, toBookmark, toHex };
13769
13776
  //# sourceMappingURL=lake.js.map