Source: control/draw.js

  1. import { Draw } from 'ol/interaction';
  2. import Control from './control';
  3. import drawPointSVG from '../../img/draw_point.svg';
  4. import drawPolygonSVG from '../../img/draw_polygon.svg';
  5. import drawLineSVG from '../../img/draw_line.svg';
  6. /**
  7. * Control for drawing features.
  8. * @extends {ole.Control}
  9. * @alias ole.DrawControl
  10. */
  11. class DrawControl extends Control {
  12. /**
  13. * @param {Object} [options] Tool options.
  14. * @param {string} [options.type] Geometry type ('Point', 'LineString', 'Polygon',
  15. * 'MultiPoint', 'MultiLineString', 'MultiPolygon' or 'Circle').
  16. * Default is 'Point'.
  17. * @param {Object} [options.drawInteractionOptions] Options for the Draw interaction (ol/interaction/Draw).
  18. * @param {ol.style.Style.StyleLike} [options.style] Style used for the draw interaction.
  19. */
  20. constructor(options) {
  21. let image = null;
  22. switch (options?.type) {
  23. case 'Polygon':
  24. image = drawPolygonSVG;
  25. break;
  26. case 'LineString':
  27. image = drawLineSVG;
  28. break;
  29. default:
  30. image = drawPointSVG;
  31. }
  32. super({
  33. title: `Draw ${options?.type || 'Point'}`,
  34. className: 'ole-control-draw',
  35. image,
  36. ...(options || {}),
  37. });
  38. /**
  39. * @type {ol.interaction.Draw}
  40. * @private
  41. */
  42. this.drawInteraction = new Draw({
  43. type: options?.type || 'Point',
  44. features: options?.features,
  45. source: options?.source,
  46. style: options?.style,
  47. stopClick: true,
  48. ...(options?.drawInteractionOptions || {}),
  49. });
  50. this.drawInteraction.on('drawstart', (evt) => {
  51. this.editor.setDrawFeature(evt.feature);
  52. });
  53. this.drawInteraction.on('drawend', () => {
  54. this.editor.setDrawFeature(null);
  55. });
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. activate() {
  61. this.map.addInteraction(this.drawInteraction);
  62. super.activate();
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. deactivate(silent) {
  68. this.map.removeInteraction(this.drawInteraction);
  69. super.deactivate(silent);
  70. }
  71. }
  72. export default DrawControl;