1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-11 21:29:03 +09:00
This commit is contained in:
こぴなたみぽ 2018-02-11 22:04:08 +09:00
parent 6cf1fdbf82
commit a0f3182a0a
2 changed files with 361 additions and 341 deletions

View File

@ -79,6 +79,11 @@ export default class MiOS extends EventEmitter {
*/
private shouldRegisterSw: boolean;
/**
*
*/
public windows = new WindowSystem();
/**
* MiOSインスタンスを作成します
* @param shouldRegisterSw ServiceWorkerを登録するかどうか
@ -359,6 +364,22 @@ export default class MiOS extends EventEmitter {
}
}
class WindowSystem {
private windows = new Set();
public add(window) {
this.windows.add(window);
}
public remove(window) {
this.windows.delete(window);
}
public getAll() {
return this.windows;
}
}
/**
* Convert the URL safe base64 string to a Uint8Array
* @param base64String base64 string

View File

@ -3,23 +3,23 @@
<div class="bg" ref="bg" v-show="isModal" @click="onBgClick"></div>
<div class="main" ref="main" tabindex="-1" :data-is-modal="isModal" @mousedown="onBodyMousedown" @keydown="onKeydown" :style="{ width, height }">
<div class="body">
<header ref="header" @mousedown="onHeaderMousedown">
<header ref="header" @contextmenu.prevent="() => {}" @mousedown.prevent="onHeaderMousedown">
<h1 data-yield="header"><yield from="header"/></h1>
<div>
<button class="popout" v-if="popoutUrl" @mousedown="repelMove" @click="popout" title="ポップアウト">%fa:R window-restore%</button>
<button class="close" v-if="canClose" @mousedown="repelMove" @click="close" title="閉じる">%fa:times%</button>
<button class="popout" v-if="popoutUrl" @mousedown.stop="() => {}" @click="popout" title="ポップアウト">%fa:R window-restore%</button>
<button class="close" v-if="canClose" @mousedown.stop="() => {}" @click="close" title="閉じる">%fa:times%</button>
</div>
</header>
<div class="content" data-yield="content"><yield from="content"/></div>
</div>
<div class="handle top" v-if="canResize" @mousedown="onTopHandleMousedown"></div>
<div class="handle right" v-if="canResize" @mousedown="onRightHandleMousedown"></div>
<div class="handle bottom" v-if="canResize" @mousedown="onBottomHandleMousedown"></div>
<div class="handle left" v-if="canResize" @mousedown="onLeftHandleMousedown"></div>
<div class="handle top-left" v-if="canResize" @mousedown="onTopLeftHandleMousedown"></div>
<div class="handle top-right" v-if="canResize" @mousedown="onTopRightHandleMousedown"></div>
<div class="handle bottom-right" v-if="canResize" @mousedown="onBottomRightHandleMousedown"></div>
<div class="handle bottom-left" v-if="canResize" @mousedown="onBottomLeftHandleMousedown"></div>
<div class="handle top" v-if="canResize" @mousedown.prevent="onTopHandleMousedown"></div>
<div class="handle right" v-if="canResize" @mousedown.prevent="onRightHandleMousedown"></div>
<div class="handle bottom" v-if="canResize" @mousedown.prevent="onBottomHandleMousedown"></div>
<div class="handle left" v-if="canResize" @mousedown.prevent="onLeftHandleMousedown"></div>
<div class="handle top-left" v-if="canResize" @mousedown.prevent="onTopLeftHandleMousedown"></div>
<div class="handle top-right" v-if="canResize" @mousedown.prevent="onTopRightHandleMousedown"></div>
<div class="handle bottom-right" v-if="canResize" @mousedown.prevent="onBottomRightHandleMousedown"></div>
<div class="handle bottom-left" v-if="canResize" @mousedown.prevent="onBottomLeftHandleMousedown"></div>
</div>
</div>
</template>
@ -32,6 +32,18 @@ import contains from '../../common/scripts/contains';
const minHeight = 40;
const minWidth = 200;
function dragListen(fn) {
window.addEventListener('mousemove', fn);
window.addEventListener('mouseleave', dragClear.bind(null, fn));
window.addEventListener('mouseup', dragClear.bind(null, fn));
}
function dragClear(fn) {
window.removeEventListener('mousemove', fn);
window.removeEventListener('mouseleave', dragClear);
window.removeEventListener('mouseup', dragClear);
}
export default Vue.extend({
props: {
isModal: {
@ -54,6 +66,7 @@ export default Vue.extend({
type: String
}
},
computed: {
isFlexible(): boolean {
return this.height == null;
@ -62,363 +75,350 @@ export default Vue.extend({
return !this.isFlexible;
}
},
created() {
//
this.$root.$data.os.windows.add(this);
},
mounted() {
const main = this.$refs.main as any;
main.style.top = '15%';
main.style.left = (window.innerWidth / 2) - (main.offsetWidth / 2) + 'px';
}
});
</script>
window.addEventListener('resize', this.onBrowserResize);
this.open();
},
<script lang="typescript">
destroyed() {
//
this.$root.$data.os.windows.remove(this);
this.on('mount', () => {
window.removeEventListener('resize', this.onBrowserResize);
},
this.$refs.main.style.top = '15%';
this.$refs.main.style.left = (window.innerWidth / 2) - (this.$refs.main.offsetWidth / 2) + 'px';
methods: {
open() {
this.$emit('opening');
this.$refs.header.addEventListener('contextmenu', e => {
e.preventDefault();
});
this.top();
window.addEventListener('resize', this.onBrowserResize);
const bg = this.$refs.bg as any;
const main = this.$refs.main as any;
this.open();
});
this.on('unmount', () => {
window.removeEventListener('resize', this.onBrowserResize);
});
this.onBrowserResize = () => {
const position = this.$refs.main.getBoundingClientRect();
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = this.$refs.main.offsetWidth;
const windowHeight = this.$refs.main.offsetHeight;
if (position.left < 0) this.$refs.main.style.left = 0;
if (position.top < 0) this.$refs.main.style.top = 0;
if (position.left + windowWidth > browserWidth) this.$refs.main.style.left = browserWidth - windowWidth + 'px';
if (position.top + windowHeight > browserHeight) this.$refs.main.style.top = browserHeight - windowHeight + 'px';
};
this.open = () => {
this.$emit('opening');
this.top();
if (this.isModal) {
this.$refs.bg.style.pointerEvents = 'auto';
anime({
targets: this.$refs.bg,
opacity: 1,
duration: 100,
easing: 'linear'
});
}
this.$refs.main.style.pointerEvents = 'auto';
anime({
targets: this.$refs.main,
opacity: 1,
scale: [1.1, 1],
duration: 200,
easing: 'easeOutQuad'
});
//this.$refs.main.focus();
setTimeout(() => {
this.$emit('opened');
}, 300);
};
this.popout = () => {
const position = this.$refs.main.getBoundingClientRect();
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const x = window.screenX + position.left;
const y = window.screenY + position.top;
const url = typeof this.popoutUrl == 'function' ? this.popoutUrl() : this.popoutUrl;
window.open(url, url,
`height=${height},width=${width},left=${x},top=${y}`);
this.close();
};
this.close = () => {
this.$emit('closing');
if (this.isModal) {
this.$refs.bg.style.pointerEvents = 'none';
anime({
targets: this.$refs.bg,
opacity: 0,
duration: 300,
easing: 'linear'
});
}
this.$refs.main.style.pointerEvents = 'none';
anime({
targets: this.$refs.main,
opacity: 0,
scale: 0.8,
duration: 300,
easing: [0.5, -0.5, 1, 0.5]
});
setTimeout(() => {
this.$emit('closed');
}, 300);
};
//
this.top = () => {
let z = 0;
const ws = document.querySelectorAll('mk-window');
ws.forEach(w => {
if (w == this.root) return;
const m = w.querySelector(':scope > .main');
const mz = Number(document.defaultView.getComputedStyle(m, null).zIndex);
if (mz > z) z = mz;
});
if (z > 0) {
this.$refs.main.style.zIndex = z + 1;
if (this.isModal) this.$refs.bg.style.zIndex = z + 1;
}
};
this.repelMove = e => {
e.stopPropagation();
return true;
};
this.bgClick = () => {
if (this.canClose) this.close();
};
this.onBodyMousedown = () => {
this.top();
};
//
this.onHeaderMousedown = e => {
e.preventDefault();
if (!contains(this.$refs.main, document.activeElement)) this.$refs.main.focus();
const position = this.$refs.main.getBoundingClientRect();
const clickX = e.clientX;
const clickY = e.clientY;
const moveBaseX = clickX - position.left;
const moveBaseY = clickY - position.top;
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = this.$refs.main.offsetWidth;
const windowHeight = this.$refs.main.offsetHeight;
//
dragListen(me => {
let moveLeft = me.clientX - moveBaseX;
let moveTop = me.clientY - moveBaseY;
//
if (moveTop < 0) moveTop = 0;
//
if (moveLeft < 0) moveLeft = 0;
//
if (moveTop + windowHeight > browserHeight) moveTop = browserHeight - windowHeight;
//
if (moveLeft + windowWidth > browserWidth) moveLeft = browserWidth - windowWidth;
this.$refs.main.style.left = moveLeft + 'px';
this.$refs.main.style.top = moveTop + 'px';
});
};
//
this.onTopHandleMousedown = e => {
e.preventDefault();
const base = e.clientY;
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const top = parseInt(getComputedStyle(this.$refs.main, '').top, 10);
//
dragListen(me => {
const move = me.clientY - base;
if (top + move > 0) {
if (height + -move > this.minHeight) {
this.applyTransformHeight(height + -move);
this.applyTransformTop(top + move);
} else { //
this.applyTransformHeight(this.minHeight);
this.applyTransformTop(top + (height - this.minHeight));
if (this.isModal) {
bg.style.pointerEvents = 'auto';
anime({
targets: bg,
opacity: 1,
duration: 100,
easing: 'linear'
});
}
} else { //
this.applyTransformHeight(top + height);
this.applyTransformTop(0);
}
});
};
//
this.onRightHandleMousedown = e => {
e.preventDefault();
main.style.pointerEvents = 'auto';
anime({
targets: main,
opacity: 1,
scale: [1.1, 1],
duration: 200,
easing: 'easeOutQuad'
});
const base = e.clientX;
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const left = parseInt(getComputedStyle(this.$refs.main, '').left, 10);
const browserWidth = window.innerWidth;
if (focus) main.focus();
//
dragListen(me => {
const move = me.clientX - base;
if (left + width + move < browserWidth) {
if (width + move > this.minWidth) {
this.applyTransformWidth(width + move);
} else { //
this.applyTransformWidth(this.minWidth);
setTimeout(() => {
this.$emit('opened');
}, 300);
},
close() {
this.$emit('closing');
const bg = this.$refs.bg as any;
const main = this.$refs.main as any;
if (this.isModal) {
bg.style.pointerEvents = 'none';
anime({
targets: bg,
opacity: 0,
duration: 300,
easing: 'linear'
});
}
} else { //
this.applyTransformWidth(browserWidth - left);
}
});
};
//
this.onBottomHandleMousedown = e => {
e.preventDefault();
main.style.pointerEvents = 'none';
const base = e.clientY;
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const top = parseInt(getComputedStyle(this.$refs.main, '').top, 10);
const browserHeight = window.innerHeight;
anime({
targets: main,
opacity: 0,
scale: 0.8,
duration: 300,
easing: [0.5, -0.5, 1, 0.5]
});
//
dragListen(me => {
const move = me.clientY - base;
if (top + height + move < browserHeight) {
if (height + move > this.minHeight) {
this.applyTransformHeight(height + move);
} else { //
this.applyTransformHeight(this.minHeight);
}
} else { //
this.applyTransformHeight(browserHeight - top);
}
});
};
setTimeout(() => {
this.$emit('closed');
}, 300);
},
//
this.onLeftHandleMousedown = e => {
e.preventDefault();
popout() {
const main = this.$refs.main as any;
const base = e.clientX;
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const left = parseInt(getComputedStyle(this.$refs.main, '').left, 10);
const position = main.getBoundingClientRect();
//
dragListen(me => {
const move = me.clientX - base;
if (left + move > 0) {
if (width + -move > this.minWidth) {
this.applyTransformWidth(width + -move);
this.applyTransformLeft(left + move);
} else { //
this.applyTransformWidth(this.minWidth);
this.applyTransformLeft(left + (width - this.minWidth));
}
} else { //
this.applyTransformWidth(left + width);
this.applyTransformLeft(0);
}
});
};
const width = parseInt(getComputedStyle(main, '').width, 10);
const height = parseInt(getComputedStyle(main, '').height, 10);
const x = window.screenX + position.left;
const y = window.screenY + position.top;
//
this.onTopLeftHandleMousedown = e => {
this.onTopHandleMousedown(e);
this.onLeftHandleMousedown(e);
};
const url = typeof this.popoutUrl == 'function' ? this.popoutUrl() : this.popoutUrl;
//
this.onTopRightHandleMousedown = e => {
this.onTopHandleMousedown(e);
this.onRightHandleMousedown(e);
};
window.open(url, url,
`height=${height}, width=${width}, left=${x}, top=${y}`);
//
this.onBottomRightHandleMousedown = e => {
this.onBottomHandleMousedown(e);
this.onRightHandleMousedown(e);
};
//
this.onBottomLeftHandleMousedown = e => {
this.onBottomHandleMousedown(e);
this.onLeftHandleMousedown(e);
};
//
this.applyTransformHeight = height => {
this.$refs.main.style.height = height + 'px';
};
//
this.applyTransformWidth = width => {
this.$refs.main.style.width = width + 'px';
};
// Y
this.applyTransformTop = top => {
this.$refs.main.style.top = top + 'px';
};
// X
this.applyTransformLeft = left => {
this.$refs.main.style.left = left + 'px';
};
function dragListen(fn) {
window.addEventListener('mousemove', fn);
window.addEventListener('mouseleave', dragClear.bind(null, fn));
window.addEventListener('mouseup', dragClear.bind(null, fn));
}
function dragClear(fn) {
window.removeEventListener('mousemove', fn);
window.removeEventListener('mouseleave', dragClear);
window.removeEventListener('mouseup', dragClear);
}
this.ondragover = e => {
e.dataTransfer.dropEffect = 'none';
};
this.onKeydown = e => {
if (e.which == 27) { // Esc
if (this.canClose) {
e.preventDefault();
e.stopPropagation();
this.close();
},
//
top() {
let z = 0;
this.$root.$data.os.windows.getAll().forEach(w => {
if (w == this) return;
const m = w.$refs.main;
const mz = Number(document.defaultView.getComputedStyle(m, null).zIndex);
if (mz > z) z = mz;
});
if (z > 0) {
(this.$refs.main as any).style.zIndex = z + 1;
if (this.isModal) (this.$refs.bg as any).style.zIndex = z + 1;
}
},
onBgClick() {
if (this.canClose) this.close();
},
onBodyMousedown() {
this.top();
},
onHeaderMousedown(e) {
const main = this.$refs.main as any;
if (!contains(main, document.activeElement)) main.focus();
const position = main.getBoundingClientRect();
const clickX = e.clientX;
const clickY = e.clientY;
const moveBaseX = clickX - position.left;
const moveBaseY = clickY - position.top;
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = main.offsetWidth;
const windowHeight = main.offsetHeight;
//
dragListen(me => {
let moveLeft = me.clientX - moveBaseX;
let moveTop = me.clientY - moveBaseY;
//
if (moveTop < 0) moveTop = 0;
//
if (moveLeft < 0) moveLeft = 0;
//
if (moveTop + windowHeight > browserHeight) moveTop = browserHeight - windowHeight;
//
if (moveLeft + windowWidth > browserWidth) moveLeft = browserWidth - windowWidth;
main.style.left = moveLeft + 'px';
main.style.top = moveTop + 'px';
});
},
//
onTopHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientY;
const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10);
//
dragListen(me => {
const move = me.clientY - base;
if (top + move > 0) {
if (height + -move > minHeight) {
this.applyTransformHeight(height + -move);
this.applyTransformTop(top + move);
} else { //
this.applyTransformHeight(minHeight);
this.applyTransformTop(top + (height - minHeight));
}
} else { //
this.applyTransformHeight(top + height);
this.applyTransformTop(0);
}
});
},
//
onRightHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientX;
const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10);
const browserWidth = window.innerWidth;
//
dragListen(me => {
const move = me.clientX - base;
if (left + width + move < browserWidth) {
if (width + move > minWidth) {
this.applyTransformWidth(width + move);
} else { //
this.applyTransformWidth(minWidth);
}
} else { //
this.applyTransformWidth(browserWidth - left);
}
});
},
//
onBottomHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientY;
const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10);
const browserHeight = window.innerHeight;
//
dragListen(me => {
const move = me.clientY - base;
if (top + height + move < browserHeight) {
if (height + move > minHeight) {
this.applyTransformHeight(height + move);
} else { //
this.applyTransformHeight(minHeight);
}
} else { //
this.applyTransformHeight(browserHeight - top);
}
});
},
//
onLeftHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientX;
const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10);
//
dragListen(me => {
const move = me.clientX - base;
if (left + move > 0) {
if (width + -move > minWidth) {
this.applyTransformWidth(width + -move);
this.applyTransformLeft(left + move);
} else { //
this.applyTransformWidth(minWidth);
this.applyTransformLeft(left + (width - minWidth));
}
} else { //
this.applyTransformWidth(left + width);
this.applyTransformLeft(0);
}
});
},
//
onTopLeftHandleMousedown(e) {
this.onTopHandleMousedown(e);
this.onLeftHandleMousedown(e);
},
//
onTopRightHandleMousedown(e) {
this.onTopHandleMousedown(e);
this.onRightHandleMousedown(e);
},
//
onBottomRightHandleMousedown(e) {
this.onBottomHandleMousedown(e);
this.onRightHandleMousedown(e);
},
//
onBottomLeftHandleMousedown(e) {
this.onBottomHandleMousedown(e);
this.onLeftHandleMousedown(e);
},
//
applyTransformHeight(height) {
(this.$refs.main as any).style.height = height + 'px';
},
//
applyTransformWidth(width) {
(this.$refs.main as any).style.width = width + 'px';
},
// Y
applyTransformTop(top) {
(this.$refs.main as any).style.top = top + 'px';
},
// X
applyTransformLeft(left) {
(this.$refs.main as any).style.left = left + 'px';
},
onDragover(e) {
e.dataTransfer.dropEffect = 'none';
},
onKeydown(e) {
if (e.which == 27) { // Esc
if (this.canClose) {
e.preventDefault();
e.stopPropagation();
this.close();
}
}
},
onBrowserResize() {
const main = this.$refs.main as any;
const position = main.getBoundingClientRect();
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = main.offsetWidth;
const windowHeight = main.offsetHeight;
if (position.left < 0) main.style.left = 0;
if (position.top < 0) main.style.top = 0;
if (position.left + windowWidth > browserWidth) main.style.left = browserWidth - windowWidth + 'px';
if (position.top + windowHeight > browserHeight) main.style.top = browserHeight - windowHeight + 'px';
}
}
};
});
</script>
<style lang="stylus" scoped>
.mk-window
display block
@ -584,4 +584,3 @@ this.onKeydown = e => {
height calc(100% - 40px)
</style>