Best JavaScript code snippet using storybook-root
views.ts
Source:views.ts
1/*---------------------------------------------------------------------------------------------2 * Copyright (c) Microsoft Corporation. All rights reserved.3 * Licensed under the MIT License. See License.txt in the project root for license information.4 *--------------------------------------------------------------------------------------------*/5import 'vs/css!./media/views';6import { Disposable } from 'vs/base/common/lifecycle';7import { TPromise } from 'vs/base/common/winjs.base';8import { IViewsService, ViewsRegistry, IViewsViewlet, ViewContainer, IViewDescriptor, IViewContainersRegistry, Extensions as ViewContainerExtensions, TEST_VIEW_CONTAINER_ID, IView, IViewDescriptorCollection } from 'vs/workbench/common/views';9import { Registry } from 'vs/platform/registry/common/platform';10import { ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet';11import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';12import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';13import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';14import { IContextKeyService, IContextKeyChangeEvent, IReadableSet, IContextKey, RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';15import { Event, chain, filterEvent, Emitter } from 'vs/base/common/event';16import { sortedDiff, firstIndex, move } from 'vs/base/common/arrays';17import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';18import { isUndefinedOrNull } from 'vs/base/common/types';19import { MenuId, MenuRegistry, ICommandAction } from 'vs/platform/actions/common/actions';20import { CommandsRegistry } from 'vs/platform/commands/common/commands';21import { localize } from 'vs/nls';22import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';23function filterViewEvent(container: ViewContainer, event: Event<IViewDescriptor[]>): Event<IViewDescriptor[]> {24 return chain(event)25 .map(views => views.filter(view => view.container === container))26 .filter(views => views.length > 0)27 .event;28}29class CounterSet<T> implements IReadableSet<T> {30 private map = new Map<T, number>();31 add(value: T): CounterSet<T> {32 this.map.set(value, (this.map.get(value) || 0) + 1);33 return this;34 }35 delete(value: T): boolean {36 let counter = this.map.get(value) || 0;37 if (counter === 0) {38 return false;39 }40 counter--;41 if (counter === 0) {42 this.map.delete(value);43 } else {44 this.map.set(value, counter);45 }46 return true;47 }48 has(value: T): boolean {49 return this.map.has(value);50 }51}52interface IViewItem {53 viewDescriptor: IViewDescriptor;54 active: boolean;55}56class ViewDescriptorCollection extends Disposable implements IViewDescriptorCollection {57 private contextKeys = new CounterSet<string>();58 private items: IViewItem[] = [];59 private _onDidChange: Emitter<{ added: IViewDescriptor[], removed: IViewDescriptor[] }> = this._register(new Emitter<{ added: IViewDescriptor[], removed: IViewDescriptor[] }>());60 readonly onDidChangeActiveViews: Event<{ added: IViewDescriptor[], removed: IViewDescriptor[] }> = this._onDidChange.event;61 get activeViewDescriptors(): IViewDescriptor[] {62 return this.items63 .filter(i => i.active)64 .map(i => i.viewDescriptor);65 }66 constructor(67 container: ViewContainer,68 @IContextKeyService private contextKeyService: IContextKeyService69 ) {70 super();71 const onRelevantViewsRegistered = filterViewEvent(container, ViewsRegistry.onViewsRegistered);72 this._register(onRelevantViewsRegistered(this.onViewsRegistered, this));73 const onRelevantViewsDeregistered = filterViewEvent(container, ViewsRegistry.onViewsDeregistered);74 this._register(onRelevantViewsDeregistered(this.onViewsDeregistered, this));75 const onRelevantContextChange = filterEvent(contextKeyService.onDidChangeContext, e => e.affectsSome(this.contextKeys));76 this._register(onRelevantContextChange(this.onContextChanged, this));77 this.onViewsRegistered(ViewsRegistry.getViews(container));78 }79 private onViewsRegistered(viewDescriptors: IViewDescriptor[]): any {80 const added: IViewDescriptor[] = [];81 for (const viewDescriptor of viewDescriptors) {82 const item = {83 viewDescriptor,84 active: this.isViewDescriptorActive(viewDescriptor) // TODO: should read from some state?85 };86 this.items.push(item);87 if (viewDescriptor.when) {88 for (const key of viewDescriptor.when.keys()) {89 this.contextKeys.add(key);90 }91 }92 if (item.active) {93 added.push(viewDescriptor);94 }95 }96 if (added.length) {97 this._onDidChange.fire({ added, removed: [] });98 }99 }100 private onViewsDeregistered(viewDescriptors: IViewDescriptor[]): any {101 const removed: IViewDescriptor[] = [];102 for (const viewDescriptor of viewDescriptors) {103 const index = firstIndex(this.items, i => i.viewDescriptor.id === viewDescriptor.id);104 if (index === -1) {105 continue;106 }107 const item = this.items[index];108 this.items.splice(index, 1);109 if (viewDescriptor.when) {110 for (const key of viewDescriptor.when.keys()) {111 this.contextKeys.delete(key);112 }113 }114 if (item.active) {115 removed.push(viewDescriptor);116 }117 }118 if (removed.length) {119 this._onDidChange.fire({ added: [], removed });120 }121 }122 private onContextChanged(event: IContextKeyChangeEvent): any {123 const removed: IViewDescriptor[] = [];124 const added: IViewDescriptor[] = [];125 for (const item of this.items) {126 const active = this.isViewDescriptorActive(item.viewDescriptor);127 if (item.active !== active) {128 if (active) {129 added.push(item.viewDescriptor);130 } else {131 removed.push(item.viewDescriptor);132 }133 }134 item.active = active;135 }136 if (added.length || removed.length) {137 this._onDidChange.fire({ added, removed });138 }139 }140 private isViewDescriptorActive(viewDescriptor: IViewDescriptor): boolean {141 return !viewDescriptor.when || this.contextKeyService.contextMatchesRules(viewDescriptor.when);142 }143}144export interface IViewState {145 visible: boolean;146 collapsed: boolean;147 order?: number;148 size?: number;149}150export interface IViewDescriptorRef {151 viewDescriptor: IViewDescriptor;152 index: number;153}154export interface IAddedViewDescriptorRef extends IViewDescriptorRef {155 collapsed: boolean;156 size?: number;157}158export class ContributableViewsModel extends Disposable {159 readonly viewDescriptors: IViewDescriptor[] = [];160 get visibleViewDescriptors(): IViewDescriptor[] {161 return this.viewDescriptors.filter(v => this.viewStates.get(v.id).visible);162 }163 private _onDidAdd = this._register(new Emitter<IAddedViewDescriptorRef[]>());164 readonly onDidAdd: Event<IAddedViewDescriptorRef[]> = this._onDidAdd.event;165 private _onDidRemove = this._register(new Emitter<IViewDescriptorRef[]>());166 readonly onDidRemove: Event<IViewDescriptorRef[]> = this._onDidRemove.event;167 private _onDidMove = this._register(new Emitter<{ from: IViewDescriptorRef; to: IViewDescriptorRef; }>());168 readonly onDidMove: Event<{ from: IViewDescriptorRef; to: IViewDescriptorRef; }> = this._onDidMove.event;169 constructor(170 container: ViewContainer,171 viewsService: IViewsService,172 protected viewStates = new Map<string, IViewState>(),173 ) {174 super();175 const viewDescriptorCollection = viewsService.getViewDescriptors(container);176 this._register(viewDescriptorCollection.onDidChangeActiveViews(() => this.onDidChangeViewDescriptors(viewDescriptorCollection.activeViewDescriptors)));177 this.onDidChangeViewDescriptors(viewDescriptorCollection.activeViewDescriptors);178 }179 isVisible(id: string): boolean {180 const state = this.viewStates.get(id);181 if (!state) {182 throw new Error(`Unknown view ${id}`);183 }184 return state.visible;185 }186 setVisible(id: string, visible: boolean): void {187 const { visibleIndex, viewDescriptor, state } = this.find(id);188 if (!viewDescriptor.canToggleVisibility) {189 throw new Error(`Can't toggle this view's visibility`);190 }191 if (state.visible === visible) {192 return;193 }194 state.visible = visible;195 if (visible) {196 this._onDidAdd.fire([{ index: visibleIndex, viewDescriptor, size: state.size, collapsed: state.collapsed }]);197 } else {198 this._onDidRemove.fire([{ index: visibleIndex, viewDescriptor }]);199 }200 }201 isCollapsed(id: string): boolean {202 const state = this.viewStates.get(id);203 if (!state) {204 throw new Error(`Unknown view ${id}`);205 }206 return state.collapsed;207 }208 setCollapsed(id: string, collapsed: boolean): void {209 const { state } = this.find(id);210 state.collapsed = collapsed;211 }212 getSize(id: string): number | undefined {213 const state = this.viewStates.get(id);214 if (!state) {215 throw new Error(`Unknown view ${id}`);216 }217 return state.size;218 }219 setSize(id: string, size: number): void {220 const { state } = this.find(id);221 state.size = size;222 }223 move(from: string, to: string): void {224 const fromIndex = firstIndex(this.viewDescriptors, v => v.id === from);225 const toIndex = firstIndex(this.viewDescriptors, v => v.id === to);226 const fromViewDescriptor = this.viewDescriptors[fromIndex];227 const toViewDescriptor = this.viewDescriptors[toIndex];228 move(this.viewDescriptors, fromIndex, toIndex);229 for (let index = 0; index < this.viewDescriptors.length; index++) {230 const state = this.viewStates.get(this.viewDescriptors[index].id);231 state.order = index;232 }233 this._onDidMove.fire({234 from: { index: fromIndex, viewDescriptor: fromViewDescriptor },235 to: { index: toIndex, viewDescriptor: toViewDescriptor }236 });237 }238 private find(id: string): { index: number, visibleIndex: number, viewDescriptor: IViewDescriptor, state: IViewState } {239 for (let i = 0, visibleIndex = 0; i < this.viewDescriptors.length; i++) {240 const viewDescriptor = this.viewDescriptors[i];241 const state = this.viewStates.get(viewDescriptor.id);242 if (viewDescriptor.id === id) {243 return { index: i, visibleIndex, viewDescriptor, state };244 }245 if (state.visible) {246 visibleIndex++;247 }248 }249 throw new Error(`view descriptor ${id} not found`);250 }251 private compareViewDescriptors(a: IViewDescriptor, b: IViewDescriptor): number {252 if (a.id === b.id) {253 return 0;254 }255 return (this.getViewOrder(a) - this.getViewOrder(b)) || (a.id < b.id ? -1 : 1);256 }257 private getViewOrder(viewDescriptor: IViewDescriptor): number {258 const viewState = this.viewStates.get(viewDescriptor.id);259 const viewOrder = viewState && typeof viewState.order === 'number' ? viewState.order : viewDescriptor.order;260 return typeof viewOrder === 'number' ? viewOrder : Number.MAX_VALUE;261 }262 private onDidChangeViewDescriptors(viewDescriptors: IViewDescriptor[]): void {263 const ids = new Set<string>();264 for (const viewDescriptor of this.viewDescriptors) {265 ids.add(viewDescriptor.id);266 }267 viewDescriptors = viewDescriptors.sort(this.compareViewDescriptors.bind(this));268 for (const viewDescriptor of viewDescriptors) {269 const viewState = this.viewStates.get(viewDescriptor.id);270 if (viewState) {271 // set defaults if not set272 viewState.visible = isUndefinedOrNull(viewState.visible) ? !viewDescriptor.hideByDefault : viewState.visible;273 viewState.collapsed = isUndefinedOrNull(viewState.collapsed) ? !!viewDescriptor.collapsed : viewState.collapsed;274 } else {275 this.viewStates.set(viewDescriptor.id, {276 visible: !viewDescriptor.hideByDefault,277 collapsed: viewDescriptor.collapsed278 });279 }280 }281 const splices = sortedDiff<IViewDescriptor>(282 this.viewDescriptors,283 viewDescriptors,284 this.compareViewDescriptors.bind(this)285 ).reverse();286 const toRemove: { index: number, viewDescriptor: IViewDescriptor }[] = [];287 const toAdd: { index: number, viewDescriptor: IViewDescriptor, size: number, collapsed: boolean }[] = [];288 for (const splice of splices) {289 const startViewDescriptor = this.viewDescriptors[splice.start];290 let startIndex = startViewDescriptor ? this.find(startViewDescriptor.id).visibleIndex : this.viewDescriptors.length;291 for (let i = 0; i < splice.deleteCount; i++) {292 const viewDescriptor = this.viewDescriptors[splice.start + i];293 const { state } = this.find(viewDescriptor.id);294 if (state.visible) {295 toRemove.push({ index: startIndex++, viewDescriptor });296 }297 }298 for (let i = 0; i < splice.toInsert.length; i++) {299 const viewDescriptor = splice.toInsert[i];300 const state = this.viewStates.get(viewDescriptor.id);301 if (state.visible) {302 toAdd.push({ index: startIndex++, viewDescriptor, size: state.size, collapsed: state.collapsed });303 }304 }305 }306 this.viewDescriptors.splice(0, this.viewDescriptors.length, ...viewDescriptors);307 if (toRemove.length) {308 this._onDidRemove.fire(toRemove);309 }310 if (toAdd.length) {311 this._onDidAdd.fire(toAdd);312 }313 }314}315export class PersistentContributableViewsModel extends ContributableViewsModel {316 private viewletStateStorageId: string;317 private readonly hiddenViewsStorageId: string;318 private storageService: IStorageService;319 private contextService: IWorkspaceContextService;320 constructor(321 container: ViewContainer,322 viewletStateStorageId: string,323 @IViewsService viewsService: IViewsService,324 @IStorageService storageService: IStorageService,325 @IWorkspaceContextService contextService: IWorkspaceContextService326 ) {327 const hiddenViewsStorageId = `${viewletStateStorageId}.hidden`;328 const viewStates = PersistentContributableViewsModel.loadViewsStates(viewletStateStorageId, hiddenViewsStorageId, storageService, contextService);329 super(container, viewsService, viewStates);330 this.viewletStateStorageId = viewletStateStorageId;331 this.hiddenViewsStorageId = hiddenViewsStorageId;332 this.storageService = storageService;333 this.contextService = contextService;334 this._register(this.onDidAdd(viewDescriptorRefs => this.saveVisibilityStates(viewDescriptorRefs.map(r => r.viewDescriptor))));335 this._register(this.onDidRemove(viewDescriptorRefs => this.saveVisibilityStates(viewDescriptorRefs.map(r => r.viewDescriptor))));336 }337 saveViewsStates(): void {338 const storedViewsStates: { [id: string]: { collapsed: boolean, size: number, order: number } } = {};339 for (const viewDescriptor of this.viewDescriptors) {340 const viewState = this.viewStates.get(viewDescriptor.id);341 if (viewState) {342 storedViewsStates[viewDescriptor.id] = { collapsed: viewState.collapsed, size: viewState.size, order: viewState.order };343 }344 }345 this.storageService.store(this.viewletStateStorageId, JSON.stringify(storedViewsStates), this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? StorageScope.WORKSPACE : StorageScope.GLOBAL);346 }347 private saveVisibilityStates(viewDescriptors: IViewDescriptor[]): void {348 const storedViewsVisibilityStates = PersistentContributableViewsModel.loadViewsVisibilityState(this.hiddenViewsStorageId, this.storageService, this.contextService);349 for (const viewDescriptor of viewDescriptors) {350 if (viewDescriptor.canToggleVisibility) {351 const viewState = this.viewStates.get(viewDescriptor.id);352 storedViewsVisibilityStates.push({ id: viewDescriptor.id, isHidden: viewState ? !viewState.visible : void 0 });353 }354 }355 this.storageService.store(this.hiddenViewsStorageId, JSON.stringify(storedViewsVisibilityStates), StorageScope.GLOBAL);356 }357 private static loadViewsStates(viewletStateStorageId: string, hiddenViewsStorageId: string, storageService: IStorageService, contextService: IWorkspaceContextService): Map<string, IViewState> {358 const viewStates = new Map<string, IViewState>();359 const storedViewsStates = JSON.parse(storageService.get(viewletStateStorageId, contextService.getWorkbenchState() !== WorkbenchState.EMPTY ? StorageScope.WORKSPACE : StorageScope.GLOBAL, '{}'));360 const viewsVisibilityStates = PersistentContributableViewsModel.loadViewsVisibilityState(hiddenViewsStorageId, storageService, contextService);361 for (const { id, isHidden } of viewsVisibilityStates) {362 const viewState = storedViewsStates[id];363 if (viewState) {364 viewStates.set(id, <IViewState>{ ...viewState, ...{ visible: !isHidden } });365 } else {366 // New workspace367 viewStates.set(id, <IViewState>{ ...{ visible: !isHidden } });368 }369 }370 for (const id of Object.keys(storedViewsStates)) {371 if (!viewStates.has(id)) {372 viewStates.set(id, <IViewState>{ ...storedViewsStates[id] });373 }374 }375 return viewStates;376 }377 private static loadViewsVisibilityState(hiddenViewsStorageId: string, storageService: IStorageService, contextService: IWorkspaceContextService): { id: string, isHidden: boolean }[] {378 const storedVisibilityStates = <Array<string | { id: string, isHidden: boolean }>>JSON.parse(storageService.get(hiddenViewsStorageId, StorageScope.GLOBAL, '[]'));379 return <{ id: string, isHidden: boolean }[]>storedVisibilityStates.map(c => typeof c === 'string' /* migration */ ? { id: c, isHidden: true } : c);380 }381 dispose(): void {382 this.saveViewsStates();383 super.dispose();384 }385}386const SCM_VIEWLET_ID = 'workbench.view.scm';387export class ViewsService extends Disposable implements IViewsService {388 _serviceBrand: any;389 private readonly viewDescriptorCollections: Map<ViewContainer, IViewDescriptorCollection>;390 private readonly activeViewContextKeys: Map<string, IContextKey<boolean>>;391 constructor(392 @ILifecycleService private lifecycleService: ILifecycleService,393 @IViewletService private viewletService: IViewletService,394 @IStorageService private storageService: IStorageService,395 @IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,396 @IContextKeyService private contextKeyService: IContextKeyService397 ) {398 super();399 this.viewDescriptorCollections = new Map<ViewContainer, IViewDescriptorCollection>();400 this.activeViewContextKeys = new Map<string, IContextKey<boolean>>();401 this.onDidRegisterViews(ViewsRegistry.getAllViews());402 this._register(ViewsRegistry.onViewsRegistered(views => this.onDidRegisterViews(views)));403 const viewContainersRegistry = Registry.as<IViewContainersRegistry>(ViewContainerExtensions.ViewContainersRegistry);404 viewContainersRegistry.all.forEach(viewContainer => this.onDidRegisterViewContainer(viewContainer));405 this._register(viewContainersRegistry.onDidRegister(viewContainer => this.onDidRegisterViewContainer(viewContainer)));406 this._register(Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets).onDidRegister(viewlet => this.viewletService.setViewletEnablement(viewlet.id, this.storageService.getBoolean(`viewservice.${viewlet.id}.enablement`, this.getStorageScope(), viewlet.id !== TEST_VIEW_CONTAINER_ID))));407 }408 getViewDescriptors(container: ViewContainer): IViewDescriptorCollection {409 return this.viewDescriptorCollections.get(container);410 }411 openView(id: string, focus: boolean): TPromise<IView> {412 const viewDescriptor = ViewsRegistry.getView(id);413 if (viewDescriptor) {414 const viewletDescriptor = this.viewletService.getViewlet(viewDescriptor.container.id);415 if (viewletDescriptor) {416 return this.viewletService.openViewlet(viewletDescriptor.id)417 .then((viewlet: IViewsViewlet) => {418 if (viewlet && viewlet.openView) {419 return viewlet.openView(id, focus);420 }421 return null;422 });423 }424 }425 return Promise.resolve(null);426 }427 private onDidRegisterViewContainer(viewContainer: ViewContainer): void {428 const viewDescriptorCollection = this.registerViewDescriptorCollection(viewContainer);429 // TODO: @Joao Remove this after moving SCM Viewlet to ViewContainerViewlet - https://github.com/Microsoft/vscode/issues/49054430 if (viewContainer.id !== SCM_VIEWLET_ID) {431 this._register(viewDescriptorCollection.onDidChangeActiveViews(() => this.updateViewletEnablement(viewContainer, viewDescriptorCollection)));432 this.lifecycleService.when(LifecyclePhase.Eventually).then(() => this.updateViewletEnablement(viewContainer, viewDescriptorCollection));433 }434 }435 private registerViewDescriptorCollection(viewContainer: ViewContainer): IViewDescriptorCollection {436 const viewDescriptorCollection = this._register(new ViewDescriptorCollection(viewContainer, this.contextKeyService));437 this.onDidChangeActiveViews({ added: viewDescriptorCollection.activeViewDescriptors, removed: [] });438 this._register(viewDescriptorCollection.onDidChangeActiveViews(changed => this.onDidChangeActiveViews(changed)));439 this.viewDescriptorCollections.set(viewContainer, viewDescriptorCollection);440 return viewDescriptorCollection;441 }442 private onDidChangeActiveViews({ added, removed }: { added: IViewDescriptor[], removed: IViewDescriptor[] }): void {443 added.forEach(viewDescriptor => this.getOrCreateActiveViewContextKey(viewDescriptor).set(true));444 removed.forEach(viewDescriptor => this.getOrCreateActiveViewContextKey(viewDescriptor).set(false));445 }446 private onDidRegisterViews(viewDescriptors: IViewDescriptor[]): void {447 for (const viewDescriptor of viewDescriptors) {448 const viewlet = this.viewletService.getViewlet(viewDescriptor.container.id);449 const command: ICommandAction = {450 id: viewDescriptor.focusCommand ? viewDescriptor.focusCommand.id : `${viewDescriptor.id}.focus`,451 title: { original: `Focus on ${viewDescriptor.name} View`, value: localize('focus view', "Focus on {0} View", viewDescriptor.name) },452 category: viewlet ? viewlet.name : localize('view category', "View"),453 };454 const when = ContextKeyExpr.has(`${viewDescriptor.id}.active`);455 CommandsRegistry.registerCommand(command.id, () => this.openView(viewDescriptor.id, true));456 MenuRegistry.appendMenuItem(MenuId.CommandPalette, {457 command,458 when459 });460 if (viewDescriptor.focusCommand && viewDescriptor.focusCommand.keybindings) {461 KeybindingsRegistry.registerKeybindingRule({462 id: command.id,463 when,464 weight: KeybindingWeight.WorkbenchContrib,465 primary: viewDescriptor.focusCommand.keybindings.primary,466 secondary: viewDescriptor.focusCommand.keybindings.secondary,467 linux: viewDescriptor.focusCommand.keybindings.linux,468 mac: viewDescriptor.focusCommand.keybindings.mac,469 win: viewDescriptor.focusCommand.keybindings.win470 });471 }472 }473 }474 private getOrCreateActiveViewContextKey(viewDescriptor: IViewDescriptor): IContextKey<boolean> {475 const activeContextKeyId = `${viewDescriptor.id}.active`;476 let contextKey = this.activeViewContextKeys.get(activeContextKeyId);477 if (!contextKey) {478 contextKey = new RawContextKey(activeContextKeyId, false).bindTo(this.contextKeyService);479 this.activeViewContextKeys.set(activeContextKeyId, contextKey);480 }481 return contextKey;482 }483 private updateViewletEnablement(viewContainer: ViewContainer, viewDescriptorCollection: IViewDescriptorCollection): void {484 const enabled = viewDescriptorCollection.activeViewDescriptors.length > 0;485 this.viewletService.setViewletEnablement(viewContainer.id, enabled);486 this.storageService.store(`viewservice.${viewContainer.id}.enablement`, enabled, this.getStorageScope());487 }488 private getStorageScope(): StorageScope {489 return this.workspaceContextService.getWorkbenchState() === WorkbenchState.EMPTY ? StorageScope.GLOBAL : StorageScope.WORKSPACE;490 }...
UpdateProps.js
Source:UpdateProps.js
1/* global _updateProps */2// eslint-disable-next-line @typescript-eslint/ban-ts-comment3// @ts-nocheck4import { processColor } from './Colors';5import { makeShareable, isConfigured } from './core';6import { Platform } from 'react-native';7import { _updatePropsJS } from './js-reanimated';8// copied from react-native/Libraries/Components/View/ReactNativeStyleAttributes9const colorProps = [10 'backgroundColor',11 'borderBottomColor',12 'borderColor',13 'borderLeftColor',14 'borderRightColor',15 'borderTopColor',16 'borderStartColor',17 'borderEndColor',18 'color',19 'shadowColor',20 'textDecorationColor',21 'tintColor',22 'textShadowColor',23 'overlayColor',24];25const ColorProperties = !isConfigured() ? [] : makeShareable(colorProps);26export const updateProps = (viewDescriptor, updates, maybeViewRef, adapters) => {27 'worklet';28 const viewName = viewDescriptor.value.name || 'RCTView';29 if (adapters) {30 adapters.forEach((adapter) => {31 adapter(updates);32 });33 }34 if (Platform.OS !== 'web') {35 Object.keys(updates).forEach((key) => {36 if (ColorProperties.indexOf(key) !== -1) {37 updates[key] = processColor(updates[key]);38 }39 });40 }41 const updatePropsInternal = typeof _updateProps === 'undefined' ? _updatePropsJS : _updateProps;42 updatePropsInternal(viewDescriptor.value.tag, viewName, updates, maybeViewRef);43};44export const updatePropsJestWrapper = (viewDescriptor, updates, maybeViewRef, adapters, animatedStyle) => {45 animatedStyle.current.value = Object.assign(Object.assign({}, animatedStyle.current.value), updates);46 updateProps(viewDescriptor, updates, maybeViewRef, adapters);47};...
UpdateProps.ts
Source:UpdateProps.ts
1/* global _updateProps */2// eslint-disable-next-line @typescript-eslint/ban-ts-comment3// @ts-nocheck4import { processColor } from './Colors';5import { makeShareable, isConfigured } from './core';6import { Platform } from 'react-native';7import { _updatePropsJS } from './js-reanimated';8// copied from react-native/Libraries/Components/View/ReactNativeStyleAttributes9const colorProps = [10 'backgroundColor',11 'borderBottomColor',12 'borderColor',13 'borderLeftColor',14 'borderRightColor',15 'borderTopColor',16 'borderStartColor',17 'borderEndColor',18 'color',19 'shadowColor',20 'textDecorationColor',21 'tintColor',22 'textShadowColor',23 'overlayColor',24];25const ColorProperties = !isConfigured() ? [] : makeShareable(colorProps);26export const updateProps = (27 viewDescriptor,28 updates,29 maybeViewRef,30 adapters31) => {32 'worklet';33 const viewName = viewDescriptor.value.name || 'RCTView';34 if (adapters) {35 adapters.forEach((adapter) => {36 adapter(updates);37 });38 }39 if (Platform.OS !== 'web') {40 Object.keys(updates).forEach((key) => {41 if (ColorProperties.indexOf(key) !== -1) {42 updates[key] = processColor(updates[key]);43 }44 });45 }46 const updatePropsInternal =47 typeof _updateProps === 'undefined' ? _updatePropsJS : _updateProps;48 updatePropsInternal(49 viewDescriptor.value.tag,50 viewName,51 updates,52 maybeViewRef53 );54};55export const updatePropsJestWrapper = (56 viewDescriptor,57 updates,58 maybeViewRef,59 adapters,60 animatedStyle61) => {62 animatedStyle.current.value = {63 ...animatedStyle.current.value,64 ...updates,65 };66 updateProps(viewDescriptor, updates, maybeViewRef, adapters);67};...
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4const stories = storiesOf('Test', module);5stories.add(6 withInfo()(() => {7 const view = viewDescriptor({8 props: {9 prop1: {10 },11 prop2: {12 },13 },14 });15 return <div>{view}</div>;16 })17);18import { viewDescriptor } from 'storybook-root-decorator';19import { storiesOf } from '@storybook/react';20import { withInfo } from '@storybook/addon-info';21const stories = storiesOf('Test', module);22stories.add(23 withInfo()(() => {24 const view = viewDescriptor({25 props: {26 prop1: {27 },28 prop2: {29 },30 },31 });32 return <div>{view}</div>;33 })34);35import { viewDescriptor } from 'storybook-root-decorator';36import { Meta, Story, Preview } from '@storybook/addon-docs/blocks';37 {() => {38 const view = viewDescriptor({39 props: {40 prop1: {41 },42 prop2: {
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4const stories = storiesOf('Test', module);5stories.addDecorator(withKnobs);6stories.add('with knobs', () => {7 const descriptor = viewDescriptor('test');8 return <div>Test View</div>;9});10{11 "parameters": {12 "knobs": {13 "test": {14 }15 }16 }17}
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root';2const { viewDescriptor } = require('storybook-root');3import { viewDescriptor } from 'storybook-root/lib/components/ViewDescriptor';4const { viewDescriptor } = require('storybook-root/lib/components/ViewDescriptor');5import { viewDescriptor } from 'storybook-root/lib/components/ViewDescriptor/ViewDescriptor';6const { viewDescriptor } = require('storybook-root/lib/components/ViewDescriptor/ViewDescriptor');7import { viewDescriptor } from 'storybook-root/lib/components/ViewDescriptor/ViewDescriptor/ViewDescriptor';8const { viewDescriptor } = require('storybook-root/lib/components/ViewDescriptor/ViewDescriptor/ViewDescriptor');9import React from 'react';10import { storiesOf } from '@storybook/react';11import { viewDescriptor } from 'storybook-root';12storiesOf('ViewDescriptor', module).add('default', () => (13 <div>{viewDescriptor({ id: 1, name: 'test' })}</div>14));15MIT © [Miguel Angel Arroyo](
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root-decorator';2storiesOf('MyComponent', module)3 .addDecorator(viewDescriptor())4 .add('default', () => <MyComponent />)5 .add('with props', () => <MyComponent {...props} />)6 .add('with more props', () => <MyComponent {...moreProps} />)7 .add('with even more props', () => <MyComponent {...evenMoreProps} />)8 .add('with even more more props', () => <MyComponent {...evenMoreMoreProps} />)9 .add('with even more more more props', () => <MyComponent {...evenMoreMoreMoreProps} />)10 .add('with even more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreProps} />)11 .add('with even more more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreMoreProps} />)12 .add('with even more more more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreMoreMoreProps} />)13 .add('with even more more more more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreMoreMoreMoreProps} />)14 .add('with even more more more more more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreMoreMoreMoreMoreProps} />)15 .add('with even more more more more more more more more more props', () => <MyComponent {...evenMoreMoreMoreMoreMoreMoreMoreMoreMoreProps} />)16 .add('with even more m
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root';2import { viewDescriptor } from 'storybook-root';3export { viewDescriptor };4import { viewDescriptor } from '../storybook-root';5import { viewDescriptor } from '../storybook-root';6export { viewDescriptor };7import { viewDescriptor } from '../storybook-root';8import { viewDescriptor } from '../storybook-root';9export { viewDescriptor };10import { viewDescriptor } from '../storybook-root';11import { viewDescriptor } from '../storybook-root';12export { viewDescriptor };13import { viewDescriptor } from '../storybook-root';14import { viewDescriptor } from '../storybook-root';15export { viewDescriptor };
Using AI Code Generation
1import { viewDescriptor } from "storybook-root";2viewDescriptor("test", () => {3 return "test";4});5import { viewDescriptor } from "storybook-root";6viewDescriptor("test", () => {7 return "test";8});9import { viewDescriptor } from "storybook-root";10viewDescriptor("test", () => {11 return "test";12});13import { viewDescriptor } from "storybook-root";14viewDescriptor("test", () => {15 return "test";16});17import { viewDescriptor } from "storybook-root";18viewDescriptor("test", () => {19 return "test";20});21import { viewDescriptor } from "storybook-root";22viewDescriptor("test", () => {23 return "test";24});25import { viewDescriptor } from "storybook-root";26viewDescriptor("test", () => {27 return "test";28});29import { viewDescriptor } from "storybook-root";30viewDescriptor("test", () => {31 return "test";32});33import { viewDescriptor } from "storybook-root";34viewDescriptor("test", () => {35 return "test";36});37import { viewDescriptor } from "storybook-root";38viewDescriptor("test", () => {39 return "test";40});41import { viewDescriptor } from "storybook-root";42viewDescriptor("test", () => {43 return "test";44});45import { viewDescriptor } from "storybook-root";46viewDescriptor("test", () => {47 return "test";48});
Using AI Code Generation
1import { viewDescriptor } from 'storybook-root';2const story = viewDescriptor('Test', 'Test Story');3story.add('Test', () => <Test />);4export default story;5import { viewDescriptor } from 'storybook-root';6const story = viewDescriptor('Test', 'Test Story');7story.add('Test', () => <Test />);8export default story;9import { viewDescriptor } from 'storybook-root';10const story = viewDescriptor('Test', 'Test Story');11story.add('Test', () => <Test />);12export default story;13import { viewDescriptor } from 'storybook-root';14const story = viewDescriptor('Test', 'Test Story');15story.add('Test', () => <Test />);16export default story;17import { viewDescriptor } from 'storybook-root';18const story = viewDescriptor('Test', 'Test Story');19story.add('Test', () => <Test />);20export default story;21import { viewDescriptor } from 'storybook-root';22const story = viewDescriptor('Test', 'Test Story');23story.add('Test', () => <Test />);24export default story;25import { viewDescriptor } from 'storybook-root';26const story = viewDescriptor('Test', 'Test Story');27story.add('Test', () => <Test />);28export default story;29import { viewDescriptor } from 'storybook-root';30const story = viewDescriptor('Test', 'Test Story');31story.add('Test', () => <Test />);32export default story;33import { viewDescriptor } from 'storybook-root';34const story = viewDescriptor('Test', 'Test Story');35story.add('Test', () => <Test />);36export default story;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!