官术网_书友最值得收藏!

Adding drag and drop support

Another nice feature you can provide for your small markdown editor application is the ability to drag and drop files onto the window. Users of your application should have the ability to drop a markdown file onto the editor's surface and have the content of the file immediately available to them. This scenario also helps us address some extra features of the Electron framework that you may use later. Let's get started:

  1. The easiest way to enable drop support for an entire web page running in Electron is to set the ondrop event handler for the body element:
      <body ondrop="dropHandler(event);">
<!-- page content -->
</body>
  1. For now, the drop handler implementation can be as simple as putting a message into the browser console's output. The most important part here is to prevent the default behavior and tell other DOM elements that we are now responsible for drop operations:
      <script>
function dropHandler(event) {
console.log('File(s) dropped');
event.preventDefault();
}
</script>
  1. Run the Chrome Developer Tools with the Console tab open and drag and drop a file from your system onto the markdown editor area:
You can find out more about drag and drop handling in HTML5 at https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop.
  1. For the next step, let's have some code that reads the content of the file that the user drags and drops, and shows the text in the browser console. Please refer to the following listing to see what the code should look like:
      function dropHandler(event) {
event.preventDefault();

if (event.dataTransfer.items) {
if (event.dataTransfer.items[0].kind === 'file') {
var file = event.dataTransfer.items[0].getAsFile();
if (file.type === 'text/markdown') {
var reader = new FileReader();
reader.onload = e => {
console.log(e.target.result);
};

reader.readAsText(file);
}
}
}
}
  1. Notice that we are filtering out dropped files by the mime type. It should be equal to the text/markdown value, meaning that you need to use files with the .md extension. Also, we only take the first file entry if the user drops multiples.
  2. Run the application, open Chrome Developer tools, and drop a markdown file onto the editor. It can be anything. In our example, it's a README.md file:

As you can see, the text of the markdown file should be present in the browser's console output.

  1. The final part of the implementation is straightforward. We already have a reference to the SimpleMDE editor instance, so the only thing we need to do is call the codemirror function in order to set the new text value, as follows:
      var reader = new FileReader();
reader.onload = e => {
// console.log(e.target.result);
editor.codemirror.setValue(e.target.result);
};

  1. Try the application once again. You should see the text from the dropped file appear directly inside the markdown editor:

Our implementation had succeeded, so feel free to clean the code from the console.log calls. Now, let's learn how we can support automatic updates with our markdown editor application.

主站蜘蛛池模板: 桃江县| 托里县| 德惠市| 麦盖提县| 兖州市| 阿拉善盟| 龙门县| 盐津县| 宣武区| 杨浦区| 奉化市| 波密县| 五家渠市| 北碚区| 吉木乃县| 永和县| 大石桥市| 吉隆县| 富宁县| 永福县| 铜梁县| 峡江县| 朝阳市| 毕节市| 内黄县| 仙居县| 海林市| 沧州市| 肥城市| 台南市| 枞阳县| 徐水县| 绥阳县| 溧阳市| 木里| 永福县| 安国市| 抚松县| 上犹县| 河西区| 靖江市|