UNPKG

th-ol-draw

Version:
1,916 lines (1,608 loc) 159 kB
/*! * author: sakitam <smilefdd@gmail.com> * th-ol-draw v0.0.9 * build-time: 2021-5-17 9:45 * LICENSE: MIT * (c) 2019-2021 undefined */ 'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var ol = require('ol'); var interaction = require('ol/interaction'); var style = require('ol/style'); var Feature = _interopDefault(require('ol/Feature')); var Draw = require('ol/interaction/Draw'); var layer = require('ol/layer'); var source = require('ol/source'); var geom = require('ol/geom'); var Polygon$1 = require('ol/geom/Polygon'); var extent_js = require('ol/extent.js'); var extent = require('ol/extent'); var color = require('ol/color'); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } const Observable = function () { this.Events = {}; this.__cnt = 0; }; Observable.hasOwnKey = Function.call.bind(Object.hasOwnProperty); Observable.slice = Function.call.bind(Array.prototype.slice); /** * 事件分发 * @param eventName * @param callback * @param context * @returns {(*|*)[]} */ Observable.prototype.on = function (eventName, callback, context) { return (this.bindEvent(eventName, callback, 0, context)) }; /** * 取消监听 * @param event * @returns {boolean} */ Observable.prototype.un = function (event) { var eventName = ''; var key = ''; var r = false; var type = typeof event; var that = this; if (type === 'string') { if (Observable.hasOwnKey(this.Events, event)) { delete this.Events[event]; return true } return false } else if (type === 'object') { eventName = event[0]; key = event[1]; if (Observable.hasOwnKey(this.Events, eventName) && Observable.hasOwnKey(this.Events[eventName], key)) { delete this.Events[eventName][key]; return true } return false } else if (type === 'function') { that.eachEvent(that.Events, function (keyA, itemA) { that.eachEvent(itemA, function (keyB, itemB) { if (itemB[0] === event) { delete that.Events[keyA][keyB]; r = true; } }); }); return r } return true }; /** * 事件监听(只触发一次) * @param eventName * @param callback * @param context * @returns {(*|*)[]} */ Observable.prototype.once = function (eventName, callback, context) { return (this.bindEvent(eventName, callback, 1, context)) }; /** * 响应事件 * @param eventName * @param args */ Observable.prototype.action = function (eventName, args) { if (Observable.hasOwnKey(this.Events, eventName)) { this.eachEvent(this.Events[eventName], function (key, item) { item[0].apply(item[2], args); if (item[1]) { delete this.Events[eventName][key]; } }); } }; /** * 实时触发响应 * @param eventName */ Observable.prototype.dispatch = function (eventName) { var that = this; var args = Observable.slice(arguments, 1); setTimeout(function () { that.action(eventName, args); }); }; /** * 延后触发响应 * @param eventName */ Observable.prototype.dispatchSync = function (eventName) { this.action(eventName, Observable.slice(arguments, 1)); }; /** * 清空发布中心 */ Observable.prototype.clear = function () { this.Events = {}; }; /** * 绑定事件 * @param eventName * @param callback * @param isOne * @param context * @returns {[*,*]} */ Observable.prototype.bindEvent = function (eventName, callback, isOne, context) { if (typeof eventName !== 'string' || typeof callback !== 'function') { throw new Error('传入的事件名称和回调函数有误!') } if (!Observable.hasOwnKey(this.Events, eventName)) { this.Events[eventName] = {}; } this.Events[eventName][++this.__cnt] = [callback, isOne, context]; return [eventName, this.__cnt] }; /** * 循环触发事件 * @param obj * @param callback */ Observable.prototype.eachEvent = function (obj, callback) { for (var key in obj) { if (Observable.hasOwnKey(obj, key)) { callback(key, obj[key]); } } }; var FITTING_COUNT = 100; var HALF_PI = Math.PI / 2; var ZERO_TOLERANCE = 0.0001; var BASE_LAYERNAME = 'ol-plot-vector-layer'; var BASE_HELP_CONTROL_POINT_ID = 'plot-helper-control-point-div'; var BASE_HELP_HIDDEN = 'plot-helper-hidden-div'; var DEF_TEXT_STYEL = { borderRadius: '2px', fontSize: '12px', outline: 0, overflow: 'hidden', boxSizing: 'border-box', border: '1px solid #eeeeee', fontFamily: 'Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Noto Sans CJK SC,WenQuanYi Micro Hei,Arial,sans-serif', color: '#010500', fontWeight: 400, padding: '3px', fontStretch: 'normal', lineHeight: 'normal', textAlign: 'left', marginLeft: 'auto', marginRight: 'auto', width: 'auto', height: 'auto', background: 'rgb(255, 255, 255)', fontStyle: '', fontVariant: '' }; var MathDistance = function MathDistance(pnt1, pnt2) { return Math.sqrt(Math.pow(pnt1[0] - pnt2[0], 2) + Math.pow(pnt1[1] - pnt2[1], 2)); }; var wholeDistance = function wholeDistance(points) { var distance = 0; if (points && Array.isArray(points) && points.length > 0) { points.forEach(function (item, index) { if (index < points.length - 1) { distance += MathDistance(item, points[index + 1]); } }); } return distance; }; var getBaseLength = function getBaseLength(points) { return Math.pow(wholeDistance(points), 0.99); }; var Mid = function Mid(point1, point2) { return [(point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2]; }; var getCircleCenterOfThreePoints = function getCircleCenterOfThreePoints(point1, point2, point3) { var pntA = [(point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2]; var pntB = [pntA[0] - point1[1] + point2[1], pntA[1] + point1[0] - point2[0]]; var pntC = [(point1[0] + point3[0]) / 2, (point1[1] + point3[1]) / 2]; var pntD = [pntC[0] - point1[1] + point3[1], pntC[1] + point1[0] - point3[0]]; return getIntersectPoint(pntA, pntB, pntC, pntD); }; var getIntersectPoint = function getIntersectPoint(pntA, pntB, pntC, pntD) { if (pntA[1] === pntB[1]) { var _f = (pntD[0] - pntC[0]) / (pntD[1] - pntC[1]); var _x = _f * (pntA[1] - pntC[1]) + pntC[0]; var _y = pntA[1]; return [_x, _y]; } if (pntC[1] === pntD[1]) { var _e = (pntB[0] - pntA[0]) / (pntB[1] - pntA[1]); var _x2 = _e * (pntC[1] - pntA[1]) + pntA[0]; var _y2 = pntC[1]; return [_x2, _y2]; } var e = (pntB[0] - pntA[0]) / (pntB[1] - pntA[1]); var f = (pntD[0] - pntC[0]) / (pntD[1] - pntC[1]); var y = (e * pntA[1] - pntA[0] - f * pntC[1] + pntC[0]) / (e - f); var x = e * y - e * pntA[1] + pntA[0]; return [x, y]; }; var getAzimuth = function getAzimuth(startPoint, endPoint) { var azimuth; var angle = Math.asin(Math.abs(endPoint[1] - startPoint[1]) / MathDistance(startPoint, endPoint)); if (endPoint[1] >= startPoint[1] && endPoint[0] >= startPoint[0]) { azimuth = angle + Math.PI; } else if (endPoint[1] >= startPoint[1] && endPoint[0] < startPoint[0]) { azimuth = Math.PI * 2 - angle; } else if (endPoint[1] < startPoint[1] && endPoint[0] < startPoint[0]) { azimuth = angle; } else if (endPoint[1] < startPoint[1] && endPoint[0] >= startPoint[0]) { azimuth = Math.PI - angle; } return azimuth; }; var getAngleOfThreePoints = function getAngleOfThreePoints(pntA, pntB, pntC) { var angle = getAzimuth(pntB, pntA) - getAzimuth(pntB, pntC); return angle < 0 ? angle + Math.PI * 2 : angle; }; var isClockWise = function isClockWise(pnt1, pnt2, pnt3) { return (pnt3[1] - pnt1[1]) * (pnt2[0] - pnt1[0]) > (pnt2[1] - pnt1[1]) * (pnt3[0] - pnt1[0]); }; var getCubicValue = function getCubicValue(t, startPnt, cPnt1, cPnt2, endPnt) { t = Math.max(Math.min(t, 1), 0); var tp = 1 - t, t2 = t * t; var t3 = t2 * t; var tp2 = tp * tp; var tp3 = tp2 * tp; var x = tp3 * startPnt[0] + 3 * tp2 * t * cPnt1[0] + 3 * tp * t2 * cPnt2[0] + t3 * endPnt[0]; var y = tp3 * startPnt[1] + 3 * tp2 * t * cPnt1[1] + 3 * tp * t2 * cPnt2[1] + t3 * endPnt[1]; return [x, y]; }; var getThirdPoint = function getThirdPoint(startPnt, endPnt, angle, distance, clockWise) { var azimuth = getAzimuth(startPnt, endPnt); var alpha = clockWise ? azimuth + angle : azimuth - angle; var dx = distance * Math.cos(alpha); var dy = distance * Math.sin(alpha); return [endPnt[0] + dx, endPnt[1] + dy]; }; var getArcPoints = function getArcPoints(center, radius, startAngle, endAngle) { var x = null, y = null, pnts = [], angleDiff = endAngle - startAngle; angleDiff = angleDiff < 0 ? angleDiff + Math.PI * 2 : angleDiff; for (var i = 0; i <= 100; i++) { var angle = startAngle + angleDiff * i / 100; x = center[0] + radius * Math.cos(angle); y = center[1] + radius * Math.sin(angle); pnts.push([x, y]); } return pnts; }; var getBisectorNormals = function getBisectorNormals(t, pnt1, pnt2, pnt3) { var normal = getNormal(pnt1, pnt2, pnt3); var bisectorNormalRight = null, bisectorNormalLeft = null, dt = null, x = null, y = null; var dist = Math.sqrt(normal[0] * normal[0] + normal[1] * normal[1]); var uX = normal[0] / dist; var uY = normal[1] / dist; var d1 = MathDistance(pnt1, pnt2); var d2 = MathDistance(pnt2, pnt3); if (dist > ZERO_TOLERANCE) { if (isClockWise(pnt1, pnt2, pnt3)) { dt = t * d1; x = pnt2[0] - dt * uY; y = pnt2[1] + dt * uX; bisectorNormalRight = [x, y]; dt = t * d2; x = pnt2[0] + dt * uY; y = pnt2[1] - dt * uX; bisectorNormalLeft = [x, y]; } else { dt = t * d1; x = pnt2[0] + dt * uY; y = pnt2[1] - dt * uX; bisectorNormalRight = [x, y]; dt = t * d2; x = pnt2[0] - dt * uY; y = pnt2[1] + dt * uX; bisectorNormalLeft = [x, y]; } } else { x = pnt2[0] + t * (pnt1[0] - pnt2[0]); y = pnt2[1] + t * (pnt1[1] - pnt2[1]); bisectorNormalRight = [x, y]; x = pnt2[0] + t * (pnt3[0] - pnt2[0]); y = pnt2[1] + t * (pnt3[1] - pnt2[1]); bisectorNormalLeft = [x, y]; } return [bisectorNormalRight, bisectorNormalLeft]; }; var getNormal = function getNormal(pnt1, pnt2, pnt3) { var dX1 = pnt1[0] - pnt2[0]; var dY1 = pnt1[1] - pnt2[1]; var d1 = Math.sqrt(dX1 * dX1 + dY1 * dY1); dX1 /= d1; dY1 /= d1; var dX2 = pnt3[0] - pnt2[0]; var dY2 = pnt3[1] - pnt2[1]; var d2 = Math.sqrt(dX2 * dX2 + dY2 * dY2); dX2 /= d2; dY2 /= d2; var uX = dX1 + dX2; var uY = dY1 + dY2; return [uX, uY]; }; var getLeftMostControlPoint = function getLeftMostControlPoint(controlPoints, t) { var _ref = [controlPoints[0], controlPoints[1], controlPoints[2], null, null], pnt1 = _ref[0], pnt2 = _ref[1], pnt3 = _ref[2], controlX = _ref[3], controlY = _ref[4]; var pnts = getBisectorNormals(0, pnt1, pnt2, pnt3); var normalRight = pnts[0]; var normal = getNormal(pnt1, pnt2, pnt3); var dist = Math.sqrt(normal[0] * normal[0] + normal[1] * normal[1]); if (dist > ZERO_TOLERANCE) { var mid = Mid(pnt1, pnt2); var pX = pnt1[0] - mid[0]; var pY = pnt1[1] - mid[1]; var d1 = MathDistance(pnt1, pnt2); var n = 2.0 / d1; var nX = -n * pY; var nY = n * pX; var a11 = nX * nX - nY * nY; var a12 = 2 * nX * nY; var a22 = nY * nY - nX * nX; var dX = normalRight[0] - mid[0]; var dY = normalRight[1] - mid[1]; controlX = mid[0] + a11 * dX + a12 * dY; controlY = mid[1] + a12 * dX + a22 * dY; } else { controlX = pnt1[0] + t * (pnt2[0] - pnt1[0]); controlY = pnt1[1] + t * (pnt2[1] - pnt1[1]); } return [controlX, controlY]; }; var getRightMostControlPoint = function getRightMostControlPoint(controlPoints, t) { var count = controlPoints.length; var pnt1 = controlPoints[count - 3]; var pnt2 = controlPoints[count - 2]; var pnt3 = controlPoints[count - 1]; var pnts = getBisectorNormals(0, pnt1, pnt2, pnt3); var normalLeft = pnts[1]; var normal = getNormal(pnt1, pnt2, pnt3); var dist = Math.sqrt(normal[0] * normal[0] + normal[1] * normal[1]); var controlX = null, controlY = null; if (dist > ZERO_TOLERANCE) { var mid = Mid(pnt2, pnt3); var pX = pnt3[0] - mid[0]; var pY = pnt3[1] - mid[1]; var d1 = MathDistance(pnt2, pnt3); var n = 2.0 / d1; var nX = -n * pY; var nY = n * pX; var a11 = nX * nX - nY * nY; var a12 = 2 * nX * nY; var a22 = nY * nY - nX * nX; var dX = normalLeft[0] - mid[0]; var dY = normalLeft[1] - mid[1]; controlX = mid[0] + a11 * dX + a12 * dY; controlY = mid[1] + a12 * dX + a22 * dY; } else { controlX = pnt3[0] + t * (pnt2[0] - pnt3[0]); controlY = pnt3[1] + t * (pnt2[1] - pnt3[1]); } return [controlX, controlY]; }; var getCurvePoints = function getCurvePoints(t, controlPoints) { var leftControl = getLeftMostControlPoint(controlPoints, t); var pnt1 = null, pnt2 = null, pnt3 = null, normals = [leftControl], points = []; for (var i = 0; i < controlPoints.length - 2; i++) { var _ref2 = [controlPoints[i], controlPoints[i + 1], controlPoints[i + 2]]; pnt1 = _ref2[0]; pnt2 = _ref2[1]; pnt3 = _ref2[2]; var normalPoints = getBisectorNormals(t, pnt1, pnt2, pnt3); normals = normals.concat(normalPoints); } var rightControl = getRightMostControlPoint(controlPoints, t); if (rightControl) { normals.push(rightControl); } for (var _i = 0; _i < controlPoints.length - 1; _i++) { pnt1 = controlPoints[_i]; pnt2 = controlPoints[_i + 1]; points.push(pnt1); for (var _t = 0; _t < FITTING_COUNT; _t++) { var pnt = getCubicValue(_t / FITTING_COUNT, pnt1, normals[_i * 2], normals[_i * 2 + 1], pnt2); points.push(pnt); } points.push(pnt2); } return points; }; var getBezierPoints = function getBezierPoints(points) { if (points.length <= 2) { return points; } else { var bezierPoints = []; var n = points.length - 1; for (var t = 0; t <= 1; t += 0.01) { var x = 0, y = 0; for (var index = 0; index <= n; index++) { var factor = getBinomialFactor(n, index); var a = Math.pow(t, index); var b = Math.pow(1 - t, n - index); x += factor * a * b * points[index][0]; y += factor * a * b * points[index][1]; } bezierPoints.push([x, y]); } bezierPoints.push(points[n]); return bezierPoints; } }; var getFactorial = function getFactorial(n) { var result = 1; switch (n) { case n <= 1: result = 1; break; case n === 2: result = 2; break; case n === 3: result = 6; break; case n === 24: result = 24; break; case n === 5: result = 120; break; default: for (var i = 1; i <= n; i++) { result *= i; } break; } return result; }; var getBinomialFactor = function getBinomialFactor(n, index) { return getFactorial(n) / (getFactorial(index) * getFactorial(n - index)); }; var getQBSplinePoints = function getQBSplinePoints(points) { if (points.length <= 2) { return points; } else { var n = 2, bSplinePoints = []; var m = points.length - n - 1; bSplinePoints.push(points[0]); for (var i = 0; i <= m; i++) { for (var t = 0; t <= 1; t += 0.05) { var x = 0, y = 0; for (var k = 0; k <= n; k++) { var factor = getQuadricBSplineFactor(k, t); x += factor * points[i + k][0]; y += factor * points[i + k][1]; } bSplinePoints.push([x, y]); } } bSplinePoints.push(points[points.length - 1]); return bSplinePoints; } }; var getQuadricBSplineFactor = function getQuadricBSplineFactor(k, t) { var res = 0; if (k === 0) { res = Math.pow(t - 1, 2) / 2; } else if (k === 1) { res = (-2 * Math.pow(t, 2) + 2 * t + 1) / 2; } else if (k === 2) { res = Math.pow(t, 2) / 2; } return res; }; var getuuid = function getuuid() { var s = [], hexDigits = '0123456789abcdef'; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = '4'; s[19] = hexDigits.substr(s[19] & 0x3 | 0x8, 1); s[8] = s[13] = s[18] = s[23] = '-'; return s.join(''); }; var isObject = function isObject(value) { var type = typeof value; return value !== null && (type === 'object' || type === 'function'); }; var merge = function merge(a, b) { for (var key in b) { if (isObject(b[key]) && isObject(a[key])) { merge(a[key], b[key]); } else { a[key] = b[key]; } } return a; }; function bindAll(fns, context) { fns.forEach(function (fn) { if (!context[fn]) { return; } context[fn] = context[fn].bind(context); }); } var getLayerByLayerName = function getLayerByLayerName(map, layerName) { try { var targetLayer = null; if (map) { var layers = map.getLayers().getArray(); targetLayer = getLayerInternal(layers, 'layerName', layerName); } return targetLayer; } catch (e) { console.log(e); } }; var getLayerInternal = function getLayerInternal(layers, key, value) { var _target = null; if (layers.length > 0) { layers.every(function (layer$1) { if (layer$1 instanceof layer.Group) { var _layers = layer$1.getLayers().getArray(); _target = getLayerInternal(_layers, key, value); if (_target) { return false; } else { return true; } } else if (layer$1.get(key) === value) { _target = layer$1; return false; } else { return true; } }); } return _target; }; var createVectorLayer = function createVectorLayer(map, layerName, params) { try { if (map) { var vectorLayer = getLayerByLayerName(map, layerName); if (!(vectorLayer instanceof layer.Vector)) { vectorLayer = null; } if (!vectorLayer) { if (params && params.create) { vectorLayer = new layer.Vector({ layerName: layerName, params: params, layerType: 'vector', source: new source.Vector({ wrapX: false }), style: new style.Style({ fill: new style.Fill({ color: 'rgba(67, 110, 238, 0.4)' }), stroke: new style.Stroke({ color: '#4781d9', width: 2 }), image: new style.Circle({ radius: 7, fill: new style.Fill({ color: '#ffcc33' }) }) }) }); } } if (map && vectorLayer) { if (params && params.hasOwnProperty('selectable')) { vectorLayer.set('selectable', params.selectable); } var _vectorLayer = getLayerByLayerName(map, layerName); if (!_vectorLayer || !(_vectorLayer instanceof layer.Vector)) { map.addLayer(vectorLayer); } } return vectorLayer; } } catch (e) { console.log(e); } }; var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var autosize = createCommonjsModule(function (module, exports) { /*! autosize 4.0.2 license: MIT http://www.jacklmoore.com/autosize */ (function (global, factory) { { factory(module, exports); } })(commonjsGlobal, function (module, exports) { var map = typeof Map === "function" ? new Map() : function () { var keys = []; var values = []; return { has: function has(key) { return keys.indexOf(key) > -1; }, get: function get(key) { return values[keys.indexOf(key)]; }, set: function set(key, value) { if (keys.indexOf(key) === -1) { keys.push(key); values.push(value); } }, delete: function _delete(key) { var index = keys.indexOf(key); if (index > -1) { keys.splice(index, 1); values.splice(index, 1); } } }; }(); var createEvent = function createEvent(name) { return new Event(name, { bubbles: true }); }; try { new Event('test'); } catch (e) { // IE does not support `new Event()` createEvent = function createEvent(name) { var evt = document.createEvent('Event'); evt.initEvent(name, true, false); return evt; }; } function assign(ta) { if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return; var heightOffset = null; var clientWidth = null; var cachedHeight = null; function init() { var style = window.getComputedStyle(ta, null); if (style.resize === 'vertical') { ta.style.resize = 'none'; } else if (style.resize === 'both') { ta.style.resize = 'horizontal'; } if (style.boxSizing === 'content-box') { heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)); } else { heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth); } // Fix when a textarea is not on document body and heightOffset is Not a Number if (isNaN(heightOffset)) { heightOffset = 0; } update(); } function changeOverflow(value) { { // Chrome/Safari-specific fix: // When the textarea y-overflow is hidden, Chrome/Safari do not reflow the text to account for the space // made available by removing the scrollbar. The following forces the necessary text reflow. var width = ta.style.width; ta.style.width = '0px'; // Force reflow: /* jshint ignore:start */ ta.offsetWidth; /* jshint ignore:end */ ta.style.width = width; } ta.style.overflowY = value; } function getParentOverflows(el) { var arr = []; while (el && el.parentNode && el.parentNode instanceof Element) { if (el.parentNode.scrollTop) { arr.push({ node: el.parentNode, scrollTop: el.parentNode.scrollTop }); } el = el.parentNode; } return arr; } function resize() { if (ta.scrollHeight === 0) { // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM. return; } var overflows = getParentOverflows(ta); var docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240) ta.style.height = ''; ta.style.height = ta.scrollHeight + heightOffset + 'px'; // used to check if an update is actually necessary on window.resize clientWidth = ta.clientWidth; // prevents scroll-position jumping overflows.forEach(function (el) { el.node.scrollTop = el.scrollTop; }); if (docTop) { document.documentElement.scrollTop = docTop; } } function update() { resize(); var styleHeight = Math.round(parseFloat(ta.style.height)); var computed = window.getComputedStyle(ta, null); // Using offsetHeight as a replacement for computed.height in IE, because IE does not account use of border-box var actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(computed.height)) : ta.offsetHeight; // The actual height not matching the style height (set via the resize method) indicates that // the max-height has been exceeded, in which case the overflow should be allowed. if (actualHeight < styleHeight) { if (computed.overflowY === 'hidden') { changeOverflow('scroll'); resize(); actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight; } } else { // Normally keep overflow set to hidden, to avoid flash of scrollbar as the textarea expands. if (computed.overflowY !== 'hidden') { changeOverflow('hidden'); resize(); actualHeight = computed.boxSizing === 'content-box' ? Math.round(parseFloat(window.getComputedStyle(ta, null).height)) : ta.offsetHeight; } } if (cachedHeight !== actualHeight) { cachedHeight = actualHeight; var evt = createEvent('autosize:resized'); try { ta.dispatchEvent(evt); } catch (err) { // Firefox will throw an error on dispatchEvent for a detached element // https://bugzilla.mozilla.org/show_bug.cgi?id=889376 } } } var pageResize = function pageResize() { if (ta.clientWidth !== clientWidth) { update(); } }; var destroy = function (style) { window.removeEventListener('resize', pageResize, false); ta.removeEventListener('input', update, false); ta.removeEventListener('keyup', update, false); ta.removeEventListener('autosize:destroy', destroy, false); ta.removeEventListener('autosize:update', update, false); Object.keys(style).forEach(function (key) { ta.style[key] = style[key]; }); map.delete(ta); }.bind(ta, { height: ta.style.height, resize: ta.style.resize, overflowY: ta.style.overflowY, overflowX: ta.style.overflowX, wordWrap: ta.style.wordWrap }); ta.addEventListener('autosize:destroy', destroy, false); // IE9 does not fire onpropertychange or oninput for deletions, // so binding to onkeyup to catch most of those events. // There is no way that I know of to detect something like 'cut' in IE9. if ('onpropertychange' in ta && 'oninput' in ta) { ta.addEventListener('keyup', update, false); } window.addEventListener('resize', pageResize, false); ta.addEventListener('input', update, false); ta.addEventListener('autosize:update', update, false); ta.style.overflowX = 'hidden'; ta.style.wordWrap = 'break-word'; map.set(ta, { destroy: destroy, update: update }); init(); } function destroy(ta) { var methods = map.get(ta); if (methods) { methods.destroy(); } } function update(ta) { var methods = map.get(ta); if (methods) { methods.update(); } } var autosize = null; // Do nothing in Node.js environment and IE8 (or lower) if (typeof window === 'undefined' || typeof window.getComputedStyle !== 'function') { autosize = function autosize(el) { return el; }; autosize.destroy = function (el) { return el; }; autosize.update = function (el) { return el; }; } else { autosize = function autosize(el, options) { if (el) { Array.prototype.forEach.call(el.length ? el : [el], function (x) { return assign(x); }); } return el; }; autosize.destroy = function (el) { if (el) { Array.prototype.forEach.call(el.length ? el : [el], destroy); } return el; }; autosize.update = function (el) { if (el) { Array.prototype.forEach.call(el.length ? el : [el], update); } return el; }; } exports.default = autosize; module.exports = exports['default']; }); }); var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g; var MOZ_HACK_REGEXP = /^moz([A-Z])/; var create = function create(tagName, className, container, id) { var el = document.createElement(tagName); el.className = className || ''; if (id) { el.id = id; } if (container) { container.appendChild(el); } return el; }; var getElement = function getElement(id) { return typeof id === 'string' ? document.getElementById(id) : id; }; var remove = function remove(el) { var parent = el.parentNode; if (parent) { parent.removeChild(el); } }; var createHidden = function createHidden(tagName, parent, id) { var element = document.createElement(tagName); element.style.display = 'none'; if (id) { element.id = id; } if (parent) { parent.appendChild(element); } return element; }; var camelCase = function camelCase(name) { return name.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) { return offset ? letter.toUpperCase() : letter; }).replace(MOZ_HACK_REGEXP, 'Moz$1'); }; var on = function () { if (document.addEventListener) { return function (element, event, handler) { if (element && event && handler) { element.addEventListener(event, handler, false); } }; } }(); var off = function () { if (document.removeEventListener) { return function (element, event, handler) { if (element && event) { element.removeEventListener(event, handler, false); } }; } }(); function hasClass(el, cls) { if (!el || !cls) return false; if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.'); if (el.classList) { return el.classList.contains(cls); } else { return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1; } } function getStyle(element, styleName) { if (!element || !styleName) return null; styleName = camelCase(styleName); if (styleName === 'float') { styleName = 'cssFloat'; } try { var computed = document.defaultView.getComputedStyle(element, ''); return element.style[styleName] || computed ? computed[styleName] : null; } catch (e) { return element.style[styleName]; } } function setStyle(element, styleName, value) { if (!element || !styleName) return; if (typeof styleName === 'object') { for (var prop in styleName) { if (styleName.hasOwnProperty(prop)) { setStyle(element, prop, styleName[prop]); } } } else { styleName = camelCase(styleName); if (styleName === 'opacity') { element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')'; } else { element.style[styleName] = value; } } } var PlotTextBox = function (_Overlay) { _inheritsLoose(PlotTextBox, _Overlay); function PlotTextBox(options) { var _this; if (options === void 0) { options = {}; } var _ref = [options['id'], options['element'], options['offset'], options['stopEvent'], options['positioning'], options['insertFirst'], options['autoPan'], options['autoPanAnimation'], options['autoPanMargin'], options['className'] ? options['className'] : 'ol-plot-text-editor'], id = _ref[0], element = _ref[1], offset = _ref[2], stopEvent = _ref[3], positioning = _ref[4], insertFirst = _ref[5], autoPan = _ref[6], autoPanAnimation = _ref[7], autoPanMargin = _ref[8], className = _ref[9]; _this = _Overlay.call(this, { id: id, element: element, stopEvent: stopEvent, insertFirst: insertFirst, autoPan: autoPan, autoPanAnimation: autoPanAnimation, autoPanMargin: autoPanMargin, className: className }) || this; _this.setOffset(offset !== undefined ? offset : [0, 0]); _this.setPositioning(positioning !== undefined ? positioning : 'center-center'); _this.mapDragPan = undefined; _this.isClick_ = false; _this.dragging_ = false; _this.isFocus_ = false; _this.options_ = options; _this._position = options['position'] && options['position'].length > 0 ? options['position'] : []; _this.handleTimer_ = null; _this.currentPixel_ = []; bindAll(['handleFocus_', 'handleBlur_', 'handleClick_', 'handleDragStart_', 'handleDragEnd_', 'handleDragDrag_', 'closeCurrentPlotText', 'handleResizeMouseDown_', 'handleResizeMouseMove_', 'handleResizeMouseUp_', 'resizeButtonMoveHandler_'], _assertThisInitialized(_this)); _this.createTextContent(options); return _this; } var _proto = PlotTextBox.prototype; _proto.createTextContent = function createTextContent(options) { var _className = options.className || 'ol-plot-text-editor'; var content = document.createElement('textarea'); content.className = _className; content.style.width = options['width'] + 'px'; content.style.height = options['height'] + 'px'; content.style.minHeight = options['minHeight'] + 'px'; content.setAttribute('id', options['id']); content.setAttribute('autofocus', true); autosize(content); on(content, 'focus', this.handleFocus_); on(content, 'blur', this.handleBlur_); on(content, 'click', this.handleClick_); on(content, 'mousedown', this.handleDragStart_); on(window, 'mouseup', this.handleDragEnd_); this.set('isPlotText', true); this.setElement(content); this.createCloseButton(options); this.createResizeButton(options); this.setPosition(this._position); this.dispatchEvent('textBoxDrawEnd', { overlay: this, element: content, uuid: options['id'] }); }; _proto.getTextAreaFromContent_ = function getTextAreaFromContent_() { var _node = ''; var childrens_ = Array.prototype.slice.call(this.element && this.element.children, 0); if (childrens_.length > 0) { childrens_.every(function (ele) { if (ele.nodeType === 1 && ele.nodeName.toLowerCase() === 'textarea') { _node = ele; return false; } else { return true; } }); } return _node; }; _proto.createCloseButton = function createCloseButton(options) { var _closeSpan = document.createElement('span'); _closeSpan.className = 'ol-plot-text-editor-close'; _closeSpan.setAttribute('data-id', options['id']); off(_closeSpan, 'click', this.closeCurrentPlotText); on(_closeSpan, 'click', this.closeCurrentPlotText); this.element.appendChild(_closeSpan); }; _proto.createResizeButton = function createResizeButton(options) { var _resizeSpan = document.createElement('span'); _resizeSpan.className = 'ol-plot-text-editor-resize'; _resizeSpan.setAttribute('data-id', options['id']); off(_resizeSpan, 'mousedown', this.handleResizeMouseDown_); off(_resizeSpan, 'mousemove', this.handleResizeMouseMove_); on(_resizeSpan, 'mousedown', this.handleResizeMouseDown_); on(_resizeSpan, 'mousemove', this.handleResizeMouseMove_); this.element.appendChild(_resizeSpan); }; _proto.resizeButtonMoveHandler_ = function resizeButtonMoveHandler_(event) { var pixel_ = event.pixel; var element_ = this.getTextAreaFromContent_(); if (pixel_.length < 1 || this.currentPixel_.length < 1 || !element_) return; var _offset = [pixel_[0] - this.currentPixel_[0], pixel_[1] - this.currentPixel_[1]]; var _size = [element_.offsetWidth, element_.offsetHeight]; var _width = _size[0] + _offset[0] * 2; var _height = _size[1] + _offset[1] * 2; setStyle(element_, 'width', _width + 'px'); setStyle(element_, 'height', _height + 'px'); this.currentPixel_ = pixel_; this.getMap().render(); }; _proto.handleResizeMouseMove_ = function handleResizeMouseMove_(event) { event.stopImmediatePropagation(); }; _proto.handleResizeMouseDown_ = function handleResizeMouseDown_(event) { if (!this.getMap()) return; this.currentPixel_ = [event.x, event.y]; this.getMap().on('pointermove', this.resizeButtonMoveHandler_); on(this.getMap().getViewport(), 'mouseup', this.handleResizeMouseUp_); }; _proto.handleResizeMouseUp_ = function handleResizeMouseUp_(event) { if (!this.getMap()) return; this.getMap().un('pointermove', this.resizeButtonMoveHandler_); off(this.getMap().getViewport(), 'mouseup', this.handleResizeMouseUp_); this.currentPixel_ = []; }; _proto.closeCurrentPlotText = function closeCurrentPlotText(event) { if (!this.getMap()) return; if (event && hasClass(event.target, 'ol-plot-text-editor-close')) { var _id = event.target.getAttribute('data-id'); if (_id) { var _overlay = this.getMap().getOverlayById(_id); if (_overlay) { this.getMap().removeOverlay(_overlay); } } } }; _proto.handleFocus_ = function handleFocus_() { this.isFocus_ = true; if (this.getMap()) { this.getMap().set('activeTextArea', this); this.getMap().dispatchEvent('activeTextArea'); } }; _proto.handleBlur_ = function handleBlur_() { this.isFocus_ = false; if (this.getMap()) { this.getMap().set('activeTextArea', null); this.getMap().set('disActiveTextArea', this); this.getMap().dispatchEvent('disActiveTextArea'); } }; _proto.handleDragStart_ = function handleDragStart_(event) { var _this2 = this; if (!this.getMap()) return; if (!this.dragging_ && this.isMoveModel() && this.isFocus_) { this.handleTimer_ = window.setTimeout(function () { window.clearTimeout(_this2.handleTimer_); _this2.handleTimer_ = null; if (!_this2.isClick_) { _this2.dragging_ = true; _this2.disableMapDragPan(); _this2.preCursor_ = _this2.element.style.cursor; on(_this2.getMap().getViewport(), 'mousemove', _this2.handleDragDrag_); on(_this2.element, 'mouseup', _this2.handleDragEnd_); } }, 300); } }; _proto.handleDragDrag_ = function handleDragDrag_(event) { if (this.dragging_) { this.element.style.cursor = 'move'; this._position = this.getMap().getCoordinateFromPixel([event.clientX, event.clientY]); this.setPosition(this._position); } }; _proto.handleDragEnd_ = function handleDragEnd_(event) { this.isClick_ = false; window.clearTimeout(this.handleTimer_); this.handleTimer_ = null; if (this.dragging_ && this.isFocus_) { this.dragging_ = false; this.enableMapDragPan(); this.element.style.cursor = this.preCursor_; off(this.getMap().getViewport(), 'mousemove', this.handleDragDrag_); off(this.element, 'mouseup', this.handleDragEnd_); } }; _proto.handleClick_ = function handleClick_(event) { if (event.target === this.element) { this.isClick_ = true; } else { this.isClick_ = false; } }; _proto.isMoveModel = function isMoveModel() { var range = window.getSelection().getRangeAt(0); return range.collapsed; }; _proto.setStyle = function setStyle$1(style) { if (style === void 0) { style = {}; } var _element = this.getTextAreaFromContent_(); if (_element) { for (var key in style) { if (style[key]) { setStyle(_element, key, style[key]); } } } }; _proto.getStyle = function getStyle$1() { var _style = {}; var _element = this.getTextAreaFromContent_(); if (_element) { for (var key in DEF_TEXT_STYEL) { _style[key] = getStyle(_element, key); } } return _style; }; _proto.setValue = function setValue(value) { var _element = this.getTextAreaFromContent_(); if (_element) { _element.value = value; if (value) { autosize.update(_element); } this.getMap().render(); } }; _proto.getValue = function getValue() { var _element = this.getTextAreaFromContent_(); if (_element) { return _element.value; } else { return ''; } }; _proto.getWidth = function getWidth() { var element_ = this.getTextAreaFromContent_(); if (element_ && element_.offsetWidth) { return element_.offsetWidth; } else { return 0; } }; _proto.getHeight = function getHeight() { var element_ = this.getTextAreaFromContent_(); if (element_ && element_.offsetHeight) { return element_.offsetHeight; } else { return 0; } }; _proto.enableMapDragPan = function enableMapDragPan() { var _map = this.getMap(); if (!_map) return; if (this.mapDragPan && this.mapDragPan instanceof interaction.DragPan) { _map.addInteraction(this.mapDragPan); delete this.mapDragPan; } }; _proto.disableMapDragPan = function disableMapDragPan() { var _this3 = this; var _map = this.getMap(); if (!_map) return; var interactions = _map.getInteractions().getArray(); interactions.every(function (item) { if (item instanceof interaction.DragPan) { _this3.mapDragPan = item; _map.removeInteraction(item); return false; } else { return true; } }); }; _proto.setMap = function setMap(map) { _Overlay.prototype.setMap.call(this, map); if (map && map instanceof ol.Map) { this.setStyle(merge(DEF_TEXT_STYEL, this.options_['style'])); this.setValue(this.options_['value']); } }; return PlotTextBox; }(ol.Overlay); var TEXTAREA = 'TextArea'; var ARC = 'Arc'; var CURVE = 'Curve'; var GATHERING_PLACE = 'GatheringPlace'; var POLYLINE = 'Polyline'; var FREEHANDLINE = 'FreeHandLine'; var POINT = 'Point'; var PENNANT = 'Pennant'; var RECTANGLE = 'RectAngle'; var CIRCLE = 'Circle'; var ELLIPSE = 'Ellipse'; var LUNE = 'Lune'; var SECTOR = 'Sector'; var CLOSED_CURVE = 'ClosedCurve'; var POLYGON = 'Polygon'; var FREE_POLYGON = 'FreePolygon'; var ATTACK_ARROW = 'AttackArrow'; var DOUBLE_ARROW = 'DoubleArrow'; var STRAIGHT_ARROW = 'StraightArrow'; var FINE_ARROW = 'FineArrow'; var ASSAULT_DIRECTION = 'AssaultDirection'; var TAILED_ATTACK_ARROW = 'TailedAttackArrow'; var SQUAD_COMBAT = 'SquadCombat'; var TAILED_SQUAD_COMBAT = 'TailedSquadCombat'; var RECTFLAG = 'RectFlag'; var TRIANGLEFLAG = 'TriangleFlag'; var CURVEFLAG = 'CurveFlag'; var PlotTypes = /*#__PURE__*/Object.freeze({ __proto__: null, TEXTAREA: TEXTAREA, ARC: ARC, CURVE: CURVE, GATHERING_PLACE: GATHERING_PLACE, POLYLINE: POLYLINE, FREEHANDLINE: FREEHANDLINE, POINT: POINT, PENNANT: PENNANT, RECTANGLE: RECTANGLE, CIRCLE: CIRCLE, ELLIPSE: ELLIPSE, LUNE: LUNE, SECTOR: SECTOR, CLOSED_CURVE: CLOSED_CURVE, POLYGON: POLYGON, FREE_POLYGON: FREE_POLYGON, ATTACK_ARROW: ATTACK_ARROW, DOUBLE_ARROW: DOUBLE_ARROW, STRAIGHT_ARROW: STRAIGHT_ARROW, FINE_ARROW: FINE_ARROW, ASSAULT_DIRECTION: ASSAULT_DIRECTION, TAILED_SQUAD_COMBAT: TAILED_SQUAD_COMBAT, TAILED_ATTACK_ARROW: TAILED_ATTACK_ARROW, SQUAD_COMBAT: SQUAD_COMBAT, RECTFLAG: RECTFLAG, TRIANGLEFLAG: TRIANGLEFLAG, CURVEFLAG: CURVEFLAG }); var Point = function (_$Point) { _inheritsLoose(Point, _$Point); function Point(coordinates, point, params) { var _this; _this = _$Point.call(this, []) || this; _this.type = POINT; _this.options = params || {}; _this.set('params', _this.options); _this.fixPointCount = 1; if (point && point.length > 0) { _this.setPoints(point); } else if (coordinates && coordinates.length > 0) { _this.setCoordinates(coordinates); } return _this; } var _proto = Point.prototype; _proto.getPlotType = function getPlotType() { return this.type; }; _proto.generate = function generate() { var pnt = this.points[0]; this.setCoordinates(pnt); }; _proto.setMap = function setMap(map) { if (map && map instanceof ol.Map) { this.map = map; } else { throw new Error('传入的不是地图对象!'); } }; _proto.getMap = function getMap() { return this.map; }; _proto.isPlot = function isPlot() { return true; }; _proto.setPoints = function setPoints(value) { this.points = !value ? [] : value; if (this.points.length >= 1) { this.generate(); } }; _proto.getPoints = function getPoints() { return this.points.slice(0); }; _proto.getPointCount = function getPointCount() { return this.points.length; }; _proto.updatePoint = function updatePoint(point, index) { if (index >= 0 && index < this.points.length) { this.points[index] = point; this.generate(); } }; _proto.updateLastPoint = function updateLastPoint(point) { this.updatePoint(point, this.points.length - 1); }; _proto.finishDrawing = function finishDrawing() {}; return Point; }(geom.Point); var Pennant = function (_Point) { _inheritsLoose(Pennant, _Point); function Pennant(coordinates, point, params) { var _this; _this = _Point.call(this, []) || this; _this.type = PENNANT; _this.options = params || {}; _this.set('params', _this.options); if (point && point.length > 0) { _this.setPoints(point); } else if (coordinates && coordinates.length > 0) { _this.setCoordinates(coordinates); } return _this; } var _proto = Pennant.prototype; _proto.getPlotType = function getPlotType() { return this.type; }; _proto.generate = function generate() { this.setCoordinates(this.points); }; _proto.setMap = function setMap(map) { if (map && map instanceof ol.Map) { this.map = map; } else { throw new Error('传入的不是地图对象!'); } }; _proto.getMap = function getMap() { return this.map; }; _proto.isPlot = function isPlot() { return true; }; _proto.setPoints = function setPoints(value) { this.points = !value ? [] : value; if (this.points.length >= 1) { this.generate(); } }; _proto.getPoints = function getPoints() { return this.points.slice(0); }; _proto.getPointCount = function getPointCount() { return this.points.length; }; _proto.updatePoint = function updatePoint(point, index) { if (index >= 0 && index < this.points.length) { this.points[index] = point; this.generate(); } }; _proto.updateLastPoint = function updateLastPoint(point) { this.updatePoint(point, this.points.length - 1); }; _proto.finishDrawing = function finishDrawing() {}; return Pennant; }(geom.Point); var Polyline = function (_LineString) { _inheritsLoose(Polyline, _LineString); function Polyline(coordinates, points, params) { var _this; _this = _LineString.call(this, []) || this; _this.type = POLYLINE; _this.freehand = false; _this.set('params', params); if (points && points.length > 0) { _this.setPoints(points); } else if (coordinates && coordinates.length > 0) { _this.setCoordinates(coordinates); } return _this; } var _proto = Polyline.prototype; _proto.getPlotType = function getPlotType() { return this.type; }; _proto.generate = function generate() { this.setCoordinates(this.points); }; _proto.setMap = function setMap(map) { if (map && map instanceof ol.Map) { this.map = map; } else { throw new Error('传入的不是地图对象!'); } }; _proto.getMap = function getMap() { return this.map; }; _proto.isPlot = function isPlot() { return true; }; _proto.setPoints = function setPoints(value) { this.points = !value ? [] : value; if (this.points.length >= 1) { this.generate(); } }; _proto.getPoints = function getPoints() { return this.points.slice(0); }; _proto.getPointCount = function getPointCount() { return this.points.length; }; _proto.updatePoint = function updatePoint(point, index) { if (index >= 0 && index < this.points.length) { this.points[index] = point; this.generate(); } }; _proto.updateLastPoint = function updateLastPoint(point) { this.updatePoint(point, this.points.length - 1); }; _proto.finishDrawing = function finishDrawing() {}; return Polyline; }(geom.LineString); var Arc = function (_LineString) { _inheritsLoose(Arc, _LineString); function Arc(coordinates, points, params) { var _this; _this = _LineString.call(this, []) || this; _this.type = ARC; _this.fixPointCount = 3; _this.set('params', params); if (points && points.length > 0) { _this.setPoints(points); } else if (coordinates && coordinates.length > 0) { _this.setCoordinates(coordinates); } return _this; } var _proto = Arc.prototype; _proto.getPlotType = function getPlotType() { return this.type; }; _proto.generate = function generate() { var count = this.getPointCount(); if (count < 2) return; if (count === 2) { this.setCoordinates(this.points); } else { var _ref = [this.points[0], this.points[1], this.points[2], null, null], pnt1 = _ref[0], pnt2 = _ref[1], pnt3 = _ref[2], startAngle = _ref[3], endAngle = _ref[4]; var center = getCircleCenterOfThreePoints(pnt1, pnt2, pnt3); var radius = MathDistance(pnt1, center); var angle1 = getAzimuth(pnt1, center); var angle2 = getAzimuth(pnt2, center); if (isClockWise(pnt1, pnt2, pnt3)) { startAngle = angle2; endAngle = angle1; } else { startAngle = angle1; endAngle = angle2; } this.setCoordinates(getArcPoints(center, radius, startAngle, endAngle)); } }; _proto.setMap = function setMap(map) { if (map && map instanceof ol.Map) { this.map = map; } else { throw new Error('传入的不是地图对象!'); } }; _proto.getMap = function getMap() { return this.map; }; _proto.isPlot = function isPlot() { return true; }; _proto.setPoints = function setPoints(value) { this.points = !value ? [] : value; if (this.points.length >= 1) { this.generate(); } }; _proto.getPoints = function getPoints() { return this.points.slice(0); }; _proto.getPointCount = function getPointCount() { return this.points.length; }; _proto.updatePoint = function updatePoint(point, index) { if (index >= 0 &&