diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 39088ad..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,12 +0,0 @@ -module.exports = { - env: { - browser: true, - es6: true, - amd: true, - commonjs: true - }, - extends: 'eslint:recommended', - rules: { - 'no-extra-semi': ['off'] - } -} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..a278c2f --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "env": { + "browser": true, + "es6": true, + "amd": true, + "commonjs": true + }, + "extends": "eslint:recommended", + "rules": { + "indent": ["error", 2], + "quotes": ["error", "single"], + "no-extra-semi": "off" + } +} diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..df64aa5 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +ko_fi: surunzi +open_collective: eruda \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d47b285 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: CI + +on: + workflow_dispatch: + push: + branches: + - 'master' + paths: + - 'src/**/*' + +jobs: + ci: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [16.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + - run: npm i + - run: npm run ci diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..c9a41f4 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,28 @@ +name: Publish to NPM + +on: + workflow_dispatch: + release: + types: [created] + +jobs: + publish: + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup Node + uses: actions/setup-node@v2 + with: + node-version: '18.x' + registry-url: 'https://registry.npmjs.org' + - name: Build eruda-orientation + run: | + npm i + npm run build + - name: Publish package on NPM + run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 3d669a5..0000000 --- a/.npmignore +++ /dev/null @@ -1,7 +0,0 @@ -.gitignore -.npmignore -.prettierignore -prettier.config.js -.eslintrc.js -.travis.yml -index.html \ No newline at end of file diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..1ddf8df --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "tabWidth": 2, + "semi": false +} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 65f2ccf..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: node_js -node_js: - - "8" -script: - - npm run ci \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 71efa27..d1b4b3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.1.0 (7 Aug 2024) + +* fix: safari not working + ## v2.0.0 (3 Jan 2019) * feat: theme support \ No newline at end of file diff --git a/README.md b/README.md index e30ad60..c0e250a 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # eruda-orientation [![NPM version][npm-image]][npm-url] -[![Build status][travis-image]][travis-url] +[![Build status][ci-image]][ci-url] [![License][license-image]][npm-url] [npm-image]: https://img.shields.io/npm/v/eruda-orientation.svg [npm-url]: https://npmjs.org/package/eruda-orientation -[travis-image]: https://img.shields.io/travis/liriliri/eruda-orientation.svg -[travis-url]: https://travis-ci.org/liriliri/eruda-orientation +[ci-image]: https://img.shields.io/github/actions/workflow/status/liriliri/eruda-orientation/main.yml?branch=master&style=flat-square +[ci-url]: https://github.com/liriliri/eruda-orientation/actions/workflows/main.yml [license-image]: https://img.shields.io/npm/l/eruda-orientation.svg Eruda plugin for testing orientation api. @@ -15,7 +15,7 @@ Eruda plugin for testing orientation api. ## Demo Browse it on your phone: -[http://eruda.liriliri.io/?plugin=orientation](http://eruda.liriliri.io/?plugin=orientation) +[https://eruda.liriliri.io/?plugin=orientation](https://eruda.liriliri.io/?plugin=orientation) ## Install diff --git a/eruda-orientation.js b/eruda-orientation.js index 7631057..257bc4d 100644 --- a/eruda-orientation.js +++ b/eruda-orientation.js @@ -1,5 +1,5 @@ // https://code.tutsplus.com/tutorials/an-introduction-to-the-device-orientation-api--cms-21067 -;(function(root, factory) { +;(function (root, factory) { if (typeof define === 'function' && define.amd) { define([], factory) } else if (typeof module === 'object' && module.exports) { @@ -7,14 +7,14 @@ } else { root.erudaOrientation = factory() } -})(this, function() { - return function(eruda) { +})(this, function () { + return function (eruda) { var Tool = eruda.Tool var util = eruda.util var Orientation = Tool.extend({ name: 'orientation', - init: function($el) { + init: function ($el) { this.callSuper(Tool, 'init', arguments) this._style = util.evalCss( [ @@ -33,7 +33,7 @@ 'table {width: 100%;}', 'table td {border: 1px solid var(--border); padding: 10px;}', '.eruda-key {width: 150px;}', - 'sup {vertical-align: super; font-size: smaller;}' + 'sup {vertical-align: super; font-size: smaller;}', ].join('.eruda-dev-tools .eruda-tools .eruda-orientation ') ) var isSupported = @@ -81,7 +81,7 @@ ' ', ' ', ' ', - '' + '', ].join('') ) this._$cube = $el.find('.eruda-cube') @@ -93,7 +93,7 @@ this._bindEvent() } }, - _bindEvent: function() { + _bindEvent: function () { var $cube = this._$cube var $coordinates = this._$coordinates var $acceleration = this._$acceleration @@ -102,7 +102,7 @@ var $interval = this._$interval var self = this - this._onDeviceorientation = function(e) { + this._onDeviceorientation = function (e) { if (!self._isShow) return $cube.css( @@ -128,7 +128,7 @@ ')' ) } - this._onDevicemotion = function(e) { + this._onDevicemotion = function (e) { if (!self._isShow) return var acceleration = e.acceleration @@ -166,18 +166,50 @@ $interval.text(e.interval + 'ms') } - window.addEventListener('deviceorientation', this._onDeviceorientation) - window.addEventListener('devicemotion', this._onDevicemotion) + + function requestOrientationPermission() { + DeviceOrientationEvent.requestPermission().then(function (response) { + if (response === 'granted') { + window.addEventListener( + 'deviceorientation', + this._onDeviceorientation + ) + $cube.off('click', requestOrientationPermission) + } + }) + } + if (DeviceOrientationEvent.requestPermission) { + $cube.on('click', requestOrientationPermission) + } else { + window.addEventListener( + 'deviceorientation', + this._onDeviceorientation + ) + } + + function requestMotionPermission() { + DeviceMotionEvent.requestPermission().then(function (response) { + if (response === 'granted') { + window.addEventListener('devicemotion', this._onDevicemotion) + $cube.off('click', requestMotionPermission) + } + }) + } + if (DeviceMotionEvent.requestPermission) { + $cube.on('click', requestMotionPermission) + } else { + window.addEventListener('devicemotion', this._onDevicemotion) + } }, - show: function() { + show: function () { this.callSuper(Tool, 'show', arguments) this._isShow = true }, - hide: function() { + hide: function () { this.callSuper(Tool, 'hide', arguments) this._isShow = false }, - destroy: function() { + destroy: function () { this.callSuper(Tool, 'destroy', arguments) util.evalCss.remove(this._style) window.removeEventListener( @@ -185,7 +217,7 @@ this._onDeviceorientation ) window.removeEventListener('devicemotion', this._onDevicemotion) - } + }, }) return new Orientation() diff --git a/index.html b/index.html index ab1f280..ac8288a 100644 --- a/index.html +++ b/index.html @@ -13,7 +13,7 @@ - +