@alicloud/console-os-browser-vm 0.0.999-consoleBase

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ import React from 'react';
2
+ import beautify from 'js-beautify';
3
+
4
+ import { storiesOf } from '@storybook/react';
5
+
6
+ import { createContext } from '../src/index';
7
+ const context = createContext( { initURL: 'https://www.example.com/home' } );
8
+
9
+ const RunCode = ( { title, code, context } ) =>
10
+ <div style={ { cssText: 'margin: 0 0 30px 0' } }>
11
+ <h3 style={ { cssText: 'margin: 5px 10px; font-size: 16px' } }>{ title }</h3>
12
+ <textarea readOnly={ true } value={ beautify.js( code ) } style={ { cssText: `width: 640px; height: ${ ( code.split( '\n' ).length - 2 ) * 20 }px; border: 1px solid rgb(238, 238, 238); border-radius: 3px; background-color: rgb(255, 255, 255); cursor: pointer; font-size: 15px; padding: 3px 10px; margin: 5px 10px; resize: no` } }></textarea>
13
+ <div><button onClick={ () => { Function( 'context', `with(context){${ code }}` )( context ) } } style={ { cssText: 'border: 1px solid rgb(238, 238, 238); border-radius: 3px; background-color: rgb(255, 255, 255); cursor: pointer; font-size: 15px; padding: 3px 10px; margin: 5px 10px' } }>Run</button></div>
14
+ </div>
15
+
16
+ storiesOf( 'Browser', module )
17
+ .add( 'location', () => {
18
+ return <div>
19
+ <pre style={ { cssText: 'display:block; margin: 30px 10px' } }>
20
+ <code>
21
+ import {'{'} createContext {'}'} from '../index';<br/>
22
+ const context = createContext( {'{'} initURL: 'https://www.example.com/home' {'}'} );<br/>
23
+ const {'{'} window, history, location {'}'} = context;
24
+ </code>
25
+ </pre>
26
+
27
+ <RunCode
28
+ context={ context }
29
+ title="设置并打印 href"
30
+ code={ `
31
+ location.href = '/about';
32
+ console.log( 'location.href:', location.href );
33
+ ` } />
34
+
35
+ <RunCode
36
+ context={ context }
37
+ title="设置 hash 并打印 href"
38
+ code={ `
39
+ location.hash = '#test-hash';
40
+ window.addEventListener( 'hashchange', event => console.log( 'hashchange:', event ) );
41
+ location.hash = '#test-hash-change';
42
+ console.log( 'location.hash:', location.hash );
43
+ console.log( 'location.href:', location.href );
44
+ ` }
45
+ />
46
+
47
+ <RunCode
48
+ context={ context }
49
+ title="设置 search"
50
+ code={ `
51
+ location.search = '?name=value';
52
+ console.log( 'location.href:', location.href );
53
+ ` }
54
+ />
55
+
56
+ <RunCode
57
+ context={ context }
58
+ title="replace 跳转"
59
+ code={ `
60
+ location.replace( '/about' );
61
+ console.log( 'location.href', location.href );
62
+ ` }
63
+ />
64
+
65
+ <RunCode
66
+ context={ context }
67
+ title="assign 跳转"
68
+ code={ `
69
+ location.assign( '/blog' );
70
+ console.log( 'location.href', location.href );
71
+ ` }
72
+ />
73
+
74
+ <RunCode
75
+ context={ context }
76
+ title="修改 origin"
77
+ code={ `
78
+ location.origin = 'www.example2.com';
79
+ console.log( 'location.href', location.href );
80
+ ` }
81
+ />
82
+
83
+ <RunCode
84
+ context={ context }
85
+ title="修改 pathname"
86
+ code={ `
87
+ location.pathname = '/home';
88
+ console.log( 'location.pathname', location.pathname );
89
+ console.log( 'location.href', location.href );
90
+ ` }
91
+ />
92
+
93
+ <RunCode
94
+ context={ context }
95
+ title="修改 port"
96
+ code={ `
97
+ console.log( 'location.port', location.port );
98
+ location.port = '8080';
99
+ console.log( 'location.port', location.port );
100
+ console.log( 'location.href', location.href );
101
+ ` }
102
+ />
103
+
104
+ <RunCode
105
+ context={ context }
106
+ title="ancestorOrigins"
107
+ code={ `
108
+ console.log( 'location.ancestorOrigins', location.ancestorOrigins );
109
+ ` }
110
+ />
111
+
112
+ <RunCode
113
+ context={ context }
114
+ title="reload"
115
+ code={ `
116
+ location.reload();
117
+ ` }
118
+ />
119
+
120
+ <RunCode
121
+ context={ context }
122
+ title="toString() and valueOf()"
123
+ code={ `
124
+ console.log( 'location.toString()', location.toString() );
125
+ console.log( 'location.valueOf()', location.valueOf() );
126
+ ` }
127
+ />
128
+ </div>
129
+ } );
@@ -0,0 +1,68 @@
1
+ import React, { useEffect } from 'react';
2
+ import axios from 'axios'
3
+
4
+ import { storiesOf } from '@storybook/react';
5
+
6
+ import { createContext } from '../src/index';
7
+ import { useState } from '@storybook/addons';
8
+
9
+ const RunCode = ( { title, code, context }) =>
10
+ (<div style={{margin: '0 0 30px 0' }}>
11
+ <h3 style={{margin:'5px 10px', fontSize: 16}}>{ title }</h3>
12
+ <div>
13
+ <button
14
+ onClick={ () => { new Function('context',`
15
+ with(context){
16
+ with(window.__CONSOLE_OS_GLOBAL_VARS_){
17
+ ${ code }
18
+ }
19
+ }` )( context ) } }
20
+ style={{
21
+ border: '1px solid rgb(238, 238, 238)',
22
+ borderRadius: 3,
23
+ backgroundColor: 'rgb(255, 255, 255)',
24
+ cursor: 'pointer',
25
+ fontSize: 15,
26
+ padding: '3px 10px',
27
+ margin: '5px 10px'
28
+ }}>
29
+ Run
30
+ </button>
31
+ </div>
32
+ </div>);
33
+
34
+ let requested = false;
35
+
36
+ storiesOf( 'strict Mode', module )
37
+ .add( 'box', () => {
38
+ const [code, setCode] = useState(null)
39
+ const [context, setContext] = useState(null)
40
+ useEffect(() => {
41
+ async function request() {
42
+ const context = await createContext( { initURL: 'https://www.example.com/home' } );
43
+ setContext(context);
44
+ const resp = await axios.get('https://g.alicdn.com/aliyun-next/endpoint/0.1.2/index.js')
45
+ setCode(resp.data);
46
+ }
47
+
48
+ if (!requested) {
49
+ request();
50
+ requested = true;
51
+ }
52
+ }, [1]);
53
+ return <div>
54
+ <pre css={ 'display:block; margin: 30px 10px'}>
55
+ <code>
56
+ import {'{'} createContext {'}'} from &apos;../index&apos;;<br/>
57
+ const context = createContext( {'{'} initURL: &apos;https://www.example.com/home&apos; {'}'} );<br/>
58
+ const {'{'} window, history, location {'}'} = context;
59
+ </code>
60
+ </pre>
61
+
62
+ {code ? <RunCode
63
+ title="Strict Mode"
64
+ context={context}
65
+ code= {code}
66
+ /> : null}
67
+ </div>
68
+ } );