feat: local command

This commit is contained in:
DarkSky
2022-07-22 18:08:30 +08:00
parent 0871fbda07
commit 9361ad8e61
9 changed files with 43 additions and 23 deletions

1
.env.local Normal file
View File

@@ -0,0 +1 @@
NX_LOCAL=true

View File

@@ -1 +0,0 @@
NX_FREE_LOGIN=true

View File

@@ -84,9 +84,9 @@ const Title = styled('div')<{ size: PreviewSize }>(({ size }) => {
const ScopedEditorWrapper = styled('div')<{ size: PreviewSize }>(({ size }) => { const ScopedEditorWrapper = styled('div')<{ size: PreviewSize }>(({ size }) => {
return { return {
width: '750px', width: '940px',
height: '750px', height: '750px',
transform: 'scale(32%) translate(-102%, -104%)', transform: 'scale(25%) translate(-145%, -145%)',
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
'&>:first-child': { '&>:first-child': {
margin: 'auto', margin: 'auto',

View File

@@ -136,7 +136,6 @@ const SourceViewContainer = styled('div')<{
borderRadius: theme.affine.shape.borderRadius, borderRadius: theme.affine.shape.borderRadius,
background: isSelected ? 'rgba(152, 172, 189, 0.1)' : 'transparent', background: isSelected ? 'rgba(152, 172, 189, 0.1)' : 'transparent',
padding: '8px', padding: '8px',
// border:
iframe: { iframe: {
width: '100%', width: '100%',
height: '100%', height: '100%',
@@ -171,6 +170,7 @@ export const SourceView: FC<Props> = props => {
<SourceViewContainer <SourceViewContainer
isSelected={isSelected} isSelected={isSelected}
scene={SCENE_CONFIG.REFLINK} scene={SCENE_CONFIG.REFLINK}
style={{ padding: '0' }}
> >
<MemoBlockPreview <MemoBlockPreview
block={block} block={block}

View File

@@ -1,5 +1,5 @@
const _regex = const _regex =
/^(https?:\/\/(localhost:4200|(nightly|app)\.affine\.pro|.*?\.ligo-virgo\.pages\.dev)\/\w{28}\/)?(affine\w{16})$/; /^(https?:\/\/(localhost:4200|(nightly|app)\.affine\.pro|.*?\.ligo-virgo\.pages\.dev)\/\w{28}\/)?(affine\w{16})(\/whiteboard)?$/;
export const isAffineUrl = (url?: string) => { export const isAffineUrl = (url?: string) => {
if (!url) return false; if (!url) return false;

View File

@@ -24,16 +24,16 @@ const waitLoading = async (key: string) => {
}; };
async function _getCurrentToken() { async function _getCurrentToken() {
if (process.env['NX_FREE_LOGIN']) { if (!process.env['NX_LOCAL']) {
return 'NX_FREE_LOGIN'; const token = await getAuth().currentUser?.getIdToken();
} if (token) return token;
const token = await getAuth().currentUser?.getIdToken(); return new Promise<string>(resolve => {
if (token) return token; getAuth().onIdTokenChanged((user: User | null) => {
return new Promise<string>(resolve => { if (user) resolve(user.getIdToken());
getAuth().onIdTokenChanged((user: User | null) => { });
if (user) resolve(user.getIdToken());
}); });
}); }
return undefined;
} }
async function _getBlockDatabase( async function _getBlockDatabase(

View File

@@ -16,6 +16,7 @@ import {
transact, transact,
encodeStateAsUpdate, encodeStateAsUpdate,
applyUpdate, applyUpdate,
snapshot,
} from 'yjs'; } from 'yjs';
import { WebsocketProvider } from '@toeverything/datasource/jwt-rpc'; import { WebsocketProvider } from '@toeverything/datasource/jwt-rpc';
@@ -62,7 +63,7 @@ async function _initWebsocketProvider(
params?: YjsInitOptions['params'] params?: YjsInitOptions['params']
): Promise<[Awareness, WebsocketProvider | undefined]> { ): Promise<[Awareness, WebsocketProvider | undefined]> {
const awareness = new Awareness(doc); const awareness = new Awareness(doc);
if (token && !process.env['NX_FREE_LOGIN']) { if (token) {
const ws = new WebsocketProvider(token, url, room, doc, { const ws = new WebsocketProvider(token, url, room, doc, {
awareness, awareness,
params, params,
@@ -129,10 +130,13 @@ async function _initYjsDatabase(
binaries binaries
).whenSynced; ).whenSynced;
const gateKeeperData = doc.getMap<YMap<string>>('gatekeeper');
const gatekeeper = new GateKeeper( const gatekeeper = new GateKeeper(
userId, userId,
doc.getMap('creators'), gateKeeperData.get('creators') ||
doc.getMap('common') gateKeeperData.set('creators', new YMap()),
gateKeeperData.get('common') || gateKeeperData.set('common', new YMap())
); );
_yjsDatabaseInstance.set(workspace, { _yjsDatabaseInstance.set(workspace, {
@@ -206,8 +210,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
this.#awareness = providers.awareness; this.#awareness = providers.awareness;
this.#gatekeeper = providers.gatekeeper; this.#gatekeeper = providers.gatekeeper;
this.#blocks = this.#doc.getMap('blocks'); const blocks = this.#doc.getMap<YMap<any>>('blocks');
this.#block_updated = this.#doc.getMap('block_updated'); this.#blocks =
blocks.get('content') || blocks.set('content', new YMap());
this.#block_updated =
blocks.get('updated') || blocks.set('updated', new YMap());
this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 }); this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 });
this.#binaries = new YjsRemoteBinaries( this.#binaries = new YjsRemoteBinaries(
providers.binariesIdb.doc.getMap(), providers.binariesIdb.doc.getMap(),
@@ -381,6 +388,18 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
this.#blocks.clear(); this.#blocks.clear();
this.#block_updated.clear(); this.#block_updated.clear();
this.#gatekeeper.clear(); this.#gatekeeper.clear();
this.#doc.getMap('blocks').clear();
this.#doc.getMap('gatekeeper').clear();
},
// eslint-disable-next-line @typescript-eslint/naming-convention
clear_old: () => {
this.#doc.getMap('block_updated').clear();
this.#doc.getMap('blocks').clear();
this.#doc.getMap('common').clear();
this.#doc.getMap('creators').clear();
},
snapshot: () => {
return snapshot(this.#doc);
}, },
}; };
} }

View File

@@ -63,7 +63,7 @@ const _useUserAndSpace = () => {
}; };
const _useUserAndSpacesForFreeLogin = () => { const _useUserAndSpacesForFreeLogin = () => {
const [loading] = useAtom(_loadingAtom); const [loading, setLoading] = useAtom(_loadingAtom);
useEffect(() => setLoading(false), []); useEffect(() => setLoading(false), []);
const BRAND_ID = 'AFFiNE'; const BRAND_ID = 'AFFiNE';
@@ -78,6 +78,6 @@ const _useUserAndSpacesForFreeLogin = () => {
loading, loading,
}; };
}; };
export const useUserAndSpaces = process.env['NX_FREE_LOGIN'] export const useUserAndSpaces = process.env['NX_LOCAL']
? _useUserAndSpacesForFreeLogin ? _useUserAndSpacesForFreeLogin
: _useUserAndSpace; : _useUserAndSpace;

View File

@@ -4,10 +4,11 @@
"license": "MIT", "license": "MIT",
"author": "AFFiNE <developer@affine.pro>", "author": "AFFiNE <developer@affine.pro>",
"scripts": { "scripts": {
"start": "nx serve ligo-virgo", "start": "env-cmd -f .env.local nx serve ligo-virgo",
"start:free": "env-cmd -f .free.env nx serve ligo-virgo", "start:affine": "nx serve ligo-virgo",
"start:keck": "nx serve keck", "start:keck": "nx serve keck",
"build": "nx build ligo-virgo", "build": "nx build ligo-virgo",
"build:local": "env-cmd -f .env.local nx build ligo-virgo",
"build:keck": "nx build keck", "build:keck": "nx build keck",
"build:analytic": "cross-env BUNDLE_ANALYZER=true nx build --skip-nx-cache", "build:analytic": "cross-env BUNDLE_ANALYZER=true nx build --skip-nx-cache",
"test": "nx run-many --target test --all", "test": "nx run-many --target test --all",