lakelib 0.1.0 → 0.1.2

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.js CHANGED
@@ -737,7 +737,7 @@ class Nodes {
737
737
  }
738
738
  return block;
739
739
  }
740
- // Traverses the first node and its parents until it finds a root element which has contenteditable="true" attribute..
740
+ // Traverses the first node and its parents until it finds a root element which has contenteditable="true" attribute.
741
741
  closestContainer() {
742
742
  return this.closest('div[contenteditable="true"]');
743
743
  }
@@ -4299,7 +4299,7 @@ class Dropdown {
4299
4299
  }
4300
4300
  }
4301
4301
 
4302
- var version = "0.1.0";
4302
+ var version = "0.1.2";
4303
4303
 
4304
4304
  // Inserts a box into the specified range.
4305
4305
  function insertBox(range, boxName, boxValue) {
@@ -4902,9 +4902,6 @@ const defaultConfig = {
4902
4902
  tabIndex: 0,
4903
4903
  indentWithTab: true,
4904
4904
  minChangeSize: 5,
4905
- imageRequestMethod: 'POST',
4906
- imageRequestAction: '/upload',
4907
- imageRequestTypes: ['image/gif', 'image/jpeg', 'image/png', 'image/svg+xml'],
4908
4905
  };
4909
4906
  class Editor {
4910
4907
  constructor(config) {
@@ -5038,7 +5035,10 @@ class Editor {
5038
5035
  }
5039
5036
  this.root = query(config.root);
5040
5037
  this.toolbar = config.toolbar;
5041
- this.config = Object.assign(Object.assign({}, defaultConfig), config);
5038
+ this.config = Object.assign({}, defaultConfig);
5039
+ for (const key of Object.keys(config)) {
5040
+ this.config[key] = config[key];
5041
+ }
5042
5042
  this.containerWrapper = query('<div class="lake-container-wrapper" />');
5043
5043
  this.container = query('<div class="lake-container" />');
5044
5044
  this.overlayContainer = query('<div class="lake-overlay" />');
@@ -5207,6 +5207,17 @@ class Editor {
5207
5207
  this.history.continue();
5208
5208
  this.history.save();
5209
5209
  }
5210
+ // Sets default config for a plugin.
5211
+ setPluginConfig(pluginName, pluginConfig) {
5212
+ if (!this.config[pluginName]) {
5213
+ this.config[pluginName] = {};
5214
+ }
5215
+ for (const key of Object.keys(pluginConfig)) {
5216
+ if (this.config[pluginName][key] === undefined) {
5217
+ this.config[pluginName][key] = pluginConfig[key];
5218
+ }
5219
+ }
5220
+ }
5210
5221
  // Sets focus on the editor area.
5211
5222
  focus() {
5212
5223
  this.container.focus();
@@ -5263,11 +5274,11 @@ class Editor {
5263
5274
  this.containerWrapper.append(this.overlayContainer);
5264
5275
  query(document.body).append(this.popupContainer);
5265
5276
  this.container.append(fragment);
5277
+ Editor.plugin.loadAll(this);
5266
5278
  if (!this.readonly) {
5267
5279
  this.bindFocusEvents();
5268
5280
  this.selection.synByBookmark();
5269
5281
  this.history.save();
5270
- Editor.plugin.loadAll(this);
5271
5282
  }
5272
5283
  Editor.box.renderAll(this);
5273
5284
  if (this.toolbar) {
@@ -6232,9 +6243,9 @@ const toolbarItems = [
6232
6243
 
6233
6244
  function uploadImage(config) {
6234
6245
  const { editor, file, onError, onSuccess } = config;
6235
- const { imageRequestMethod, imageRequestAction, imageRequestTypes } = editor.config;
6236
- if (imageRequestTypes.indexOf(file.type) < 0) {
6237
- throw new Error(`Cannot upload file because its type '${file.type}' is not found in ['${imageRequestTypes.join('\', \'')}'].`);
6246
+ const { requestMethod, requestAction, requestTypes } = editor.config.image;
6247
+ if (requestTypes.indexOf(file.type) < 0) {
6248
+ throw new Error(`Cannot upload file because its type '${file.type}' is not found in ['${requestTypes.join('\', \'')}'].`);
6238
6249
  }
6239
6250
  const box = editor.insertBox('image', {
6240
6251
  url: URL.createObjectURL(file),
@@ -6278,8 +6289,8 @@ function uploadImage(config) {
6278
6289
  }
6279
6290
  },
6280
6291
  file,
6281
- action: imageRequestAction,
6282
- method: imageRequestMethod,
6292
+ action: requestAction,
6293
+ method: requestMethod,
6283
6294
  });
6284
6295
  box.setData('xhr', xhr);
6285
6296
  return box;
@@ -6980,7 +6991,6 @@ const imageBox = {
6980
6991
  };
6981
6992
 
6982
6993
  const config = {
6983
- defaultLang: 'text',
6984
6994
  comment: '#57606a',
6985
6995
  name: '#444d56',
6986
6996
  variableName: '#953800',
@@ -7075,19 +7085,22 @@ const codeBlockBox = {
7075
7085
  return;
7076
7086
  }
7077
7087
  // begin to create CodeMirror
7078
- const CodeMirror = window.CodeMirror;
7088
+ const CodeMirror = window.LakeCodeMirror;
7079
7089
  if (!CodeMirror) {
7080
7090
  codeBlockNode.addClass('lake-code-block-error');
7081
7091
  codeBlockNode.text(`
7082
- The code cannot be displayed because window.CodeMirror is not found.
7083
- Please check if the CodeMirror file is added to this page.
7092
+ The code cannot be displayed because window.LakeCodeMirror is not found.
7093
+ Please check if the "lake-codemirror" library is added to this page.
7084
7094
  `.trim());
7085
7095
  codeBlockNode.on('click', () => {
7086
7096
  editor.selection.range.selectBox(box.node);
7087
7097
  });
7088
7098
  return;
7089
7099
  }
7090
- const { EditorState, Compartment, EditorView, keymap, history, defaultKeymap, historyKeymap, indentWithTab, syntaxHighlighting, langItems, } = CodeMirror;
7100
+ const { EditorState, Compartment, EditorView, keymap, history, defaultKeymap, historyKeymap, indentWithTab, syntaxHighlighting, } = CodeMirror;
7101
+ const defaultLangItems = CodeMirror.langItems;
7102
+ const codeBlockConfig = editor.config.codeBlock;
7103
+ const langItems = defaultLangItems.filter((item) => codeBlockConfig.langList.indexOf(item.value) >= 0);
7091
7104
  // language menu items
7092
7105
  const langItemMap = new Map();
7093
7106
  for (const item of langItems) {
@@ -7133,7 +7146,7 @@ const codeBlockBox = {
7133
7146
  root: codeBlockNode,
7134
7147
  name: 'langType',
7135
7148
  downIcon: icons.get('down'),
7136
- defaultValue: langItem ? boxValue.lang : config.defaultLang,
7149
+ defaultValue: langItem ? boxValue.lang : codeBlockConfig.defaultLang,
7137
7150
  tooltip: locale.codeBlock.langType(),
7138
7151
  width: 'auto',
7139
7152
  menuType: 'list',
@@ -7162,6 +7175,9 @@ const codeBlockBox = {
7162
7175
  };
7163
7176
 
7164
7177
  var copy = (editor) => {
7178
+ if (editor.readonly) {
7179
+ return;
7180
+ }
7165
7181
  editor.container.on('copy', event => {
7166
7182
  const range = editor.selection.range;
7167
7183
  if (range.isInsideBox) {
@@ -7186,6 +7202,9 @@ var copy = (editor) => {
7186
7202
  };
7187
7203
 
7188
7204
  var cut = (editor) => {
7205
+ if (editor.readonly) {
7206
+ return;
7207
+ }
7189
7208
  editor.container.on('cut', event => {
7190
7209
  const range = editor.selection.range;
7191
7210
  if (range.isInsideBox) {
@@ -7366,8 +7385,11 @@ function pasteFragment(editor, fragment) {
7366
7385
  editor.history.save();
7367
7386
  }
7368
7387
  var paste = (editor) => {
7369
- const { imageRequestTypes } = editor.config;
7388
+ if (editor.readonly) {
7389
+ return;
7390
+ }
7370
7391
  editor.container.on('paste', event => {
7392
+ const { requestTypes } = editor.config.image;
7371
7393
  const range = editor.selection.range;
7372
7394
  if (range.isInsideBox) {
7373
7395
  return;
@@ -7381,7 +7403,7 @@ var paste = (editor) => {
7381
7403
  // upload file
7382
7404
  if (dataTransfer.files.length > 0) {
7383
7405
  for (const file of dataTransfer.files) {
7384
- if (imageRequestTypes.indexOf(file.type) >= 0) {
7406
+ if (requestTypes.indexOf(file.type) >= 0) {
7385
7407
  uploadImage({
7386
7408
  editor,
7387
7409
  file,
@@ -7412,6 +7434,9 @@ var paste = (editor) => {
7412
7434
  };
7413
7435
 
7414
7436
  var undo = (editor) => {
7437
+ if (editor.readonly) {
7438
+ return;
7439
+ }
7415
7440
  editor.command.add('undo', {
7416
7441
  execute: () => {
7417
7442
  editor.history.undo();
@@ -7428,6 +7453,9 @@ var undo = (editor) => {
7428
7453
  };
7429
7454
 
7430
7455
  var redo = (editor) => {
7456
+ if (editor.readonly) {
7457
+ return;
7458
+ }
7431
7459
  editor.command.add('redo', {
7432
7460
  execute: () => {
7433
7461
  editor.history.redo();
@@ -7446,6 +7474,9 @@ var redo = (editor) => {
7446
7474
  };
7447
7475
 
7448
7476
  var selectAll = (editor) => {
7477
+ if (editor.readonly) {
7478
+ return;
7479
+ }
7449
7480
  editor.command.add('selectAll', {
7450
7481
  execute: () => {
7451
7482
  const range = editor.selection.range;
@@ -7456,6 +7487,9 @@ var selectAll = (editor) => {
7456
7487
  };
7457
7488
 
7458
7489
  var heading = (editor) => {
7490
+ if (editor.readonly) {
7491
+ return;
7492
+ }
7459
7493
  editor.command.add('heading', {
7460
7494
  selectedValues: appliedItems => {
7461
7495
  const currentItem = appliedItems.find(item => item.node.isHeading || item.name === 'p');
@@ -7477,6 +7511,9 @@ const typeList = [
7477
7511
  'danger',
7478
7512
  ];
7479
7513
  var blockQuote = (editor) => {
7514
+ if (editor.readonly) {
7515
+ return;
7516
+ }
7480
7517
  editor.command.add('blockQuote', {
7481
7518
  isSelected: appliedItems => !!appliedItems.find(item => item.name === 'blockquote'),
7482
7519
  execute: (type) => {
@@ -7504,6 +7541,9 @@ function setChecklist(editor, value) {
7504
7541
  editor.selection.setBlocks(`<ul type="checklist"><li value="${value}"></li></ul>`);
7505
7542
  }
7506
7543
  var list = (editor) => {
7544
+ if (editor.readonly) {
7545
+ return;
7546
+ }
7507
7547
  editor.command.add('list', {
7508
7548
  selectedValues: appliedItems => {
7509
7549
  let currentValue;
@@ -7604,6 +7644,9 @@ const alignValueMap = {
7604
7644
  end: 'right',
7605
7645
  };
7606
7646
  var align = (editor) => {
7647
+ if (editor.readonly) {
7648
+ return;
7649
+ }
7607
7650
  editor.command.add('align', {
7608
7651
  selectedValues: appliedItems => {
7609
7652
  let currentValue;
@@ -7628,6 +7671,9 @@ var align = (editor) => {
7628
7671
  };
7629
7672
 
7630
7673
  var indent = (editor) => {
7674
+ if (editor.readonly) {
7675
+ return;
7676
+ }
7631
7677
  editor.command.add('indent', {
7632
7678
  execute: (type) => {
7633
7679
  const blocks = editor.selection.range.getBlocks();
@@ -7641,6 +7687,9 @@ var indent = (editor) => {
7641
7687
 
7642
7688
  const tagName$6 = 'strong';
7643
7689
  var bold = (editor) => {
7690
+ if (editor.readonly) {
7691
+ return;
7692
+ }
7644
7693
  editor.command.add('bold', {
7645
7694
  isDisabled: appliedItems => !!appliedItems.find(item => item.node.isHeading),
7646
7695
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$6),
@@ -7662,6 +7711,9 @@ var bold = (editor) => {
7662
7711
 
7663
7712
  const tagName$5 = 'i';
7664
7713
  var italic = (editor) => {
7714
+ if (editor.readonly) {
7715
+ return;
7716
+ }
7665
7717
  editor.command.add('italic', {
7666
7718
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$5),
7667
7719
  execute: () => {
@@ -7682,6 +7734,9 @@ var italic = (editor) => {
7682
7734
 
7683
7735
  const tagName$4 = 'u';
7684
7736
  var underline = (editor) => {
7737
+ if (editor.readonly) {
7738
+ return;
7739
+ }
7685
7740
  editor.command.add('underline', {
7686
7741
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$4),
7687
7742
  execute: () => {
@@ -7702,6 +7757,9 @@ var underline = (editor) => {
7702
7757
 
7703
7758
  const tagName$3 = 's';
7704
7759
  var strikethrough = (editor) => {
7760
+ if (editor.readonly) {
7761
+ return;
7762
+ }
7705
7763
  editor.command.add('strikethrough', {
7706
7764
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$3),
7707
7765
  execute: () => {
@@ -7722,6 +7780,9 @@ var strikethrough = (editor) => {
7722
7780
 
7723
7781
  const tagName$2 = 'sub';
7724
7782
  var subscript = (editor) => {
7783
+ if (editor.readonly) {
7784
+ return;
7785
+ }
7725
7786
  editor.command.add('subscript', {
7726
7787
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$2),
7727
7788
  execute: () => {
@@ -7738,6 +7799,9 @@ var subscript = (editor) => {
7738
7799
 
7739
7800
  const tagName$1 = 'sup';
7740
7801
  var superscript = (editor) => {
7802
+ if (editor.readonly) {
7803
+ return;
7804
+ }
7741
7805
  editor.command.add('superscript', {
7742
7806
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName$1),
7743
7807
  execute: () => {
@@ -7754,6 +7818,9 @@ var superscript = (editor) => {
7754
7818
 
7755
7819
  const tagName = 'code';
7756
7820
  var code = (editor) => {
7821
+ if (editor.readonly) {
7822
+ return;
7823
+ }
7757
7824
  editor.command.add('code', {
7758
7825
  isSelected: appliedItems => !!appliedItems.find(item => item.name === tagName),
7759
7826
  execute: () => {
@@ -7769,6 +7836,9 @@ var code = (editor) => {
7769
7836
  };
7770
7837
 
7771
7838
  var fontFamily = (editor) => {
7839
+ if (editor.readonly) {
7840
+ return;
7841
+ }
7772
7842
  editor.command.add('fontFamily', {
7773
7843
  selectedValues: appliedItems => {
7774
7844
  for (const item of appliedItems) {
@@ -7787,6 +7857,9 @@ var fontFamily = (editor) => {
7787
7857
  };
7788
7858
 
7789
7859
  var fontSize = (editor) => {
7860
+ if (editor.readonly) {
7861
+ return;
7862
+ }
7790
7863
  editor.command.add('fontSize', {
7791
7864
  isDisabled: appliedItems => !!appliedItems.find(item => item.node.isHeading),
7792
7865
  selectedValues: appliedItems => {
@@ -7806,6 +7879,9 @@ var fontSize = (editor) => {
7806
7879
  };
7807
7880
 
7808
7881
  var fontColor = (editor) => {
7882
+ if (editor.readonly) {
7883
+ return;
7884
+ }
7809
7885
  editor.command.add('fontColor', {
7810
7886
  selectedValues: appliedItems => {
7811
7887
  for (const item of appliedItems) {
@@ -7824,6 +7900,9 @@ var fontColor = (editor) => {
7824
7900
  };
7825
7901
 
7826
7902
  var highlight = (editor) => {
7903
+ if (editor.readonly) {
7904
+ return;
7905
+ }
7827
7906
  editor.command.add('highlight', {
7828
7907
  selectedValues: appliedItems => {
7829
7908
  for (const item of appliedItems) {
@@ -7842,6 +7921,9 @@ var highlight = (editor) => {
7842
7921
  };
7843
7922
 
7844
7923
  var removeFormat = (editor) => {
7924
+ if (editor.readonly) {
7925
+ return;
7926
+ }
7845
7927
  editor.command.add('removeFormat', {
7846
7928
  execute: () => {
7847
7929
  editor.selection.removeMark();
@@ -7852,6 +7934,9 @@ var removeFormat = (editor) => {
7852
7934
 
7853
7935
  const formatPainterClassName = 'lake-format-painter';
7854
7936
  var formatPainter = (editor) => {
7937
+ if (editor.readonly) {
7938
+ return;
7939
+ }
7855
7940
  let markList = [];
7856
7941
  editor.command.add('formatPainter', {
7857
7942
  execute: () => {
@@ -8116,6 +8201,9 @@ class LinkPopup {
8116
8201
  }
8117
8202
 
8118
8203
  var link = (editor) => {
8204
+ if (editor.readonly) {
8205
+ return;
8206
+ }
8119
8207
  const popup = new LinkPopup(editor.popupContainer);
8120
8208
  popup.event.on('save', node => {
8121
8209
  const range = editor.selection.range;
@@ -8167,6 +8255,9 @@ var link = (editor) => {
8167
8255
  };
8168
8256
 
8169
8257
  var hr = (editor) => {
8258
+ if (editor.readonly) {
8259
+ return;
8260
+ }
8170
8261
  editor.event.on('beforepaste', (nativeFragment) => {
8171
8262
  const fragment = new Fragment(nativeFragment);
8172
8263
  fragment.find('hr').each(nativeNode => {
@@ -8184,6 +8275,13 @@ var hr = (editor) => {
8184
8275
  };
8185
8276
 
8186
8277
  var image = (editor) => {
8278
+ editor.setPluginConfig('image', {
8279
+ requestMethod: 'POST',
8280
+ requestTypes: ['image/gif', 'image/jpeg', 'image/png', 'image/svg+xml'],
8281
+ });
8282
+ if (editor.readonly) {
8283
+ return;
8284
+ }
8187
8285
  editor.event.on('beforepaste', (nativeFragment) => {
8188
8286
  const fragment = new Fragment(nativeFragment);
8189
8287
  fragment.find('img').each(nativeNode => {
@@ -8210,8 +8308,35 @@ var image = (editor) => {
8210
8308
  });
8211
8309
  };
8212
8310
 
8311
+ const langList = [
8312
+ 'text',
8313
+ 'c',
8314
+ 'csharp',
8315
+ 'cpp',
8316
+ 'css',
8317
+ 'go',
8318
+ 'html',
8319
+ 'java',
8320
+ 'javascript',
8321
+ 'json',
8322
+ 'markdown',
8323
+ 'php',
8324
+ 'python',
8325
+ 'rust',
8326
+ 'sql',
8327
+ 'typescript',
8328
+ 'xml',
8329
+ 'yaml',
8330
+ ];
8213
8331
  var codeBlock = (editor) => {
8214
- if (!window.CodeMirror) {
8332
+ if (!window.LakeCodeMirror) {
8333
+ return;
8334
+ }
8335
+ editor.setPluginConfig('codeBlock', {
8336
+ langList,
8337
+ defaultLang: 'text',
8338
+ });
8339
+ if (editor.readonly) {
8215
8340
  return;
8216
8341
  }
8217
8342
  editor.command.add('codeBlock', {
@@ -8490,6 +8615,9 @@ function enterKeyExecutesBlockCommand(editor, block) {
8490
8615
  return false;
8491
8616
  }
8492
8617
  var markdown = (editor) => {
8618
+ if (editor.readonly) {
8619
+ return;
8620
+ }
8493
8621
  editor.keystroke.setKeydown('space', event => {
8494
8622
  const selection = editor.selection;
8495
8623
  const range = selection.range;
@@ -8594,6 +8722,9 @@ function addBlockOrSplitBlockForBox(editor) {
8594
8722
  }
8595
8723
  }
8596
8724
  var enterKey = (editor) => {
8725
+ if (editor.readonly) {
8726
+ return;
8727
+ }
8597
8728
  editor.keystroke.setKeydown('enter', event => {
8598
8729
  const range = editor.selection.range;
8599
8730
  if (range.isInsideBox) {
@@ -8677,6 +8808,9 @@ function addBlockOrLineBreakForBox(editor) {
8677
8808
  }
8678
8809
  }
8679
8810
  var shiftEnterKey = (editor) => {
8811
+ if (editor.readonly) {
8812
+ return;
8813
+ }
8680
8814
  editor.keystroke.setKeydown('shift+enter', event => {
8681
8815
  const range = editor.selection.range;
8682
8816
  if (range.isInsideBox) {
@@ -8739,6 +8873,9 @@ function mergeWithPreviousBlock(editor, block) {
8739
8873
  editor.selection.fixList();
8740
8874
  }
8741
8875
  var backspaceKey = (editor) => {
8876
+ if (editor.readonly) {
8877
+ return;
8878
+ }
8742
8879
  editor.keystroke.setKeydown('backspace', event => {
8743
8880
  const range = editor.selection.range;
8744
8881
  if (range.isInsideBox) {
@@ -8856,6 +8993,9 @@ function mergeWithNextBlock(editor, block) {
8856
8993
  editor.selection.fixList();
8857
8994
  }
8858
8995
  var deleteKey = (editor) => {
8996
+ if (editor.readonly) {
8997
+ return;
8998
+ }
8859
8999
  editor.keystroke.setKeydown('delete', event => {
8860
9000
  const range = editor.selection.range;
8861
9001
  if (range.isInsideBox) {
@@ -8934,6 +9074,9 @@ var deleteKey = (editor) => {
8934
9074
  };
8935
9075
 
8936
9076
  var tabKey = (editor) => {
9077
+ if (editor.readonly) {
9078
+ return;
9079
+ }
8937
9080
  editor.keystroke.setKeydown('tab', event => {
8938
9081
  if (editor.config.indentWithTab === false) {
8939
9082
  return;
@@ -8952,6 +9095,9 @@ var tabKey = (editor) => {
8952
9095
  };
8953
9096
 
8954
9097
  var arrowKeys = (editor) => {
9098
+ if (editor.readonly) {
9099
+ return;
9100
+ }
8955
9101
  editor.keystroke.setKeydown('arrow-left', event => {
8956
9102
  const range = editor.selection.range;
8957
9103
  if (range.isInsideBox) {
@@ -9073,6 +9219,9 @@ var arrowKeys = (editor) => {
9073
9219
  };
9074
9220
 
9075
9221
  var escapeKey = (editor) => {
9222
+ if (editor.readonly) {
9223
+ return;
9224
+ }
9076
9225
  editor.keystroke.setKeydown('escape', event => {
9077
9226
  const selection = editor.selection;
9078
9227
  const range = selection.range;