1- /*
1+ /**
22Usage:
3- <ClientOnly >
4- <MojsInteractive
5- id="unique_id"
6- :controller=true
7- code=
3+ <MojsInteractive
4+ id="unique_id"
5+ code=
86"new mojs.Shape ({
97 parent: '#unique_id ',
108 shape: 'circle',
119 radius: {20: 80},
1210})"
13- >
14- </MojsInteractive>
15- </ClientOnly >
11+ />
12+
13+ or if you wanna declare a height or a controller:
14+
15+ <MojsInteractive
16+ id="unique_id"
17+ :controller=true
18+ height="200px"
19+ code=
20+ "new mojs.Shape({
21+ parent: '#unique_id',
22+ shape: 'circle',
23+ radius: {20: 80},
24+ })"
25+ />
1626*/
27+
1728<template>
1829 <div class="mojs-interactive">
19- <div class =" mojs-interactive__code" >
30+ <div
31+ class="mojs-interactive__code"
32+ :class="{ 'mojs-interactive__code--pinned': isPinned }"
33+ >
2034 <prism-editor :code="code" language="js" @change="change"></prism-editor>
35+ <button class="button button--icon button--pin" v-on:click="pin" aria-label="Pin the code on scroll">
36+ {{ isPinned ? "✖️" : "📍" }}
37+ </button>
2138 <div class="buttons">
2239 <button class="button button--secondary" v-on:click="reset">Reset</button>
2340 <button class="button" v-on:click="updateCode">Update code</button>
2441 </div>
2542 </div>
26- <div :id =" this.id" class =" mojs-interactive__result" :style =" style" >
27- <button class =" button button--icon" v-if =" isPlaying && !controller" v-on:click =" pause" aria-label =" Pause animation" >⑊</button >
28- <button class =" button button--icon" v-if =" !isPlaying && !controller" v-on:click =" play" aria-label =" Play animation" >▶︎</button >
43+ <div
44+ :id="this.id"
45+ class="mojs-interactive__result"
46+ :style="style"
47+ >
48+ <button class="button button--icon button--control" v-if="!controller" v-on:click="playPause" :aria-label="isPlaying ? 'Pause animation' : 'Play animation'">{{isPlaying ? '⑊' : '︎︎︎▶︎'}}</button>
49+ <!-- <button class="button button--icon button--control" v-if="!isPlaying && !controller" v-on:click="play" aria-label="Play animation">▶︎</button> -->
2950 <div v-if="controller" :id="this.id + '_controller'" class="mojs-interactive__controller"></div>
3051 </div>
3152 </div>
3253</template>
3354
3455<script>
35- import mojs from ' mo-js' ;
36- import MojsPlayer from ' mojs-player' ;
37-
3856 import prism from 'prismjs';
3957 import PrismEditor from 'vue-prism-editor'
4058
5472 return {
5573 rawCode: this.code,
5674 isPlaying: false,
75+ isPinned: false,
5776 }
5877 },
5978
@@ -85,8 +104,17 @@ Usage:
85104 const func = new Function('window["demo_' + this.id + '"] = ' + code);
86105 try {
87106 func();
88- if (! this .controller ) {
89- window [' demo_' + this .id ].play ();
107+ if (!this.controller) {
108+ this.timeline = new mojs.Timeline({
109+ onPlaybackComplete: () => {
110+ this.isPlaying = false;
111+ }
112+ })
113+ .add(
114+ window['demo_' + this.id]
115+ )
116+ .play();
117+
90118 this.isPlaying = true;
91119 }
92120 }
@@ -119,21 +147,28 @@ Usage:
119147 this.handleCode(this.code);
120148 },
121149
122- pause : function () {
123- window [' demo_' + this .id ].pause ();
124- this .isPlaying = false ;
150+ playPause: function() {
151+ if (this.isPlaying) {
152+ this.timeline.pause();
153+ } else {
154+ this.timeline.play();
155+
156+ }
157+ this.isPlaying = !this.isPlaying;
125158 },
126159
127- play : function () {
128- window [' demo_' + this .id ].play ();
129- this .isPlaying = true ;
160+ pin: function() {
161+ this.isPinned = !this.isPinned;
130162 }
131163 },
132164
133- mounted : function () {
134- this .handleCode (this .code );
165+ mounted () {
166+ import('mo-js').then(module => {
167+ import('mojs-player').then(module => {
168+ this.handleCode(this.code);
169+ });
170+ });
135171 }
136-
137172 }
138173</script>
139174
@@ -147,15 +182,35 @@ Usage:
147182 bottom: 1rem;
148183 right: 1rem;
149184}
185+ /* TODO: Add the pinned to the container, co both the code and the code exmple can be side by side.
186+ And add a placeholder block where the old elements had been so the text doesnt jump.
187+ */
188+ .mojs-interactive__code.mojs-interactive__code--pinned {
189+ position: fixed;
190+ top: 0;
191+ right: 0;
192+ left: 0;
193+ z-index: 200;
194+ background: #2b062a;
195+ }
196+ .mojs-interactive__code.mojs-interactive__code--pinned .prism-editor-wrapper {
197+ max-height: 50vh;
198+ overflow: auto;
199+ }
200+ .mojs-interactive__code .button--pin {
201+ position: absolute;
202+ top: 0;
203+ right: 0;
204+ }
205+
150206.mojs-interactive__result {
151207 position: relative;
152208}
153- .mojs-interactive__result .button {
209+ .mojs-interactive__result .button--control {
154210 position: absolute;
155211 bottom: 0;
156212 right: 0;
157213}
158-
159214.mojs-interactive__result {
160215 background: #f1e2d7;
161216 width: 100%;
0 commit comments