Skip to content

Commit

Permalink
Merge branch 'staging' into l10n_staging
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 10, 2019
2 parents 494ff4e + c0c47d1 commit 51ddcba
Show file tree
Hide file tree
Showing 53 changed files with 476 additions and 240 deletions.
36 changes: 27 additions & 9 deletions chrome-ext-files/js/background.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
/* global chrome */
(function () {
const MAX_EXT_LOAD_COUNT = 30;
var curTabId = null;
let curTab = {
id: null,
url: null,
};

const getExtensionId = () => {
const matches = chrome.runtime.getURL('x').match(/.*\/\/(.*)\/x$/);
if (matches) {
// https://stackoverflow.com/a/47060021/3929126
// Mozilla uses an internal UUID on every installation to prevent fingerprinting
return matches[1];
}
return chrome.runtime.id;
};

// Create a new tab for the extension
function createNewTab() {
chrome.tabs.create({ url: 'index.html' }, function (tab) {
curTabId = tab.id;
curTab = {
id: tab.id,
url: tab.url
};

// Handle donation logic
handleDonation();
// handleDonation();
});
}

Expand Down Expand Up @@ -61,12 +78,13 @@

// Open the extension tab when the extension icon is clicked
chrome.browserAction.onClicked.addListener(function (tab) {
if (!curTabId) {
if (!curTab || !curTab.id) {
createNewTab();
} else {
chrome.tabs.get(curTabId, function (tab) {
if (tab) {
focusTab(curTabId);
chrome.tabs.get(curTab.id, function (tab) {
console.log(chrome.runtime.id, tab.url);
if (tab && tab.url && tab.url.includes(getExtensionId())) {
focusTab(curTab.id);
} else {
createNewTab();
}
Expand All @@ -76,8 +94,8 @@

// When a tab is closed, check if it is the extension tab that was closed, and unset curTabId
chrome.tabs.onRemoved.addListener(function (tabId) {
if (tabId === curTabId) {
curTabId = null;
if (tabId === curTab.id) {
curTab = {};
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/app/actions/gql-schema/gql-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action } from '@ngrx/store';
import { GraphQLSchema } from 'graphql';

export const SET_INTROSPECTION = 'SET_INTROSPECTION';
export const SET_INTROSPECTION_FROM_DB = 'SET_INTROSPECTION_FROM_DB';
Expand All @@ -25,7 +26,7 @@ export class SetIntrospectionFromDbAction implements Action {
export class SetSchemaAction implements Action {
readonly type = SET_SCHEMA;

constructor(public windowId: string, public payload: any) { }
constructor(public windowId: string, public payload: GraphQLSchema) { }
}

export class SetSchemaSDLAction implements Action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EventEmitter,
ChangeDetectionStrategy
} from '@angular/core';
import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
import { GraphQLInterfaceType, GraphQLObjectType, GraphQLSchema } from 'graphql';

@Component({
selector: 'app-doc-viewer-type',
Expand All @@ -17,7 +17,7 @@ import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
export class DocViewerTypeComponent implements OnInit {

@Input() data: any = {};
@Input() gqlSchema = null;
@Input() gqlSchema: GraphQLSchema;
@Output() goToFieldChange = new EventEmitter();
@Output() goToTypeChange = new EventEmitter();
@Output() addToEditorChange = new EventEmitter();
Expand All @@ -37,9 +37,9 @@ export class DocViewerTypeComponent implements OnInit {
}

switch (type) {
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType().name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType().name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType().name:
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType()!.name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType()!.name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType()!.name:
return true;
}

Expand Down
88 changes: 64 additions & 24 deletions src/app/components/doc-viewer/doc-viewer/doc-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ import { debug } from 'app/utils/logger';
import { DomSanitizer } from '@angular/platform-browser';

import { untilDestroyed } from 'ngx-take-until-destroy';
import { GraphQLType, GraphQLArgs, GraphQLSchema, GraphQLObjectType, GraphQLInterfaceType } from 'graphql';

interface DocumentView {
view: string;
parentType: string;
name: string;
}

interface DocumentIndexEntry {
search: string;
name: string;
description: string;
args: GraphQLArgs,
cat: string;
type: string;
isQuery: Boolean;
highlight: string;
}

@Component({
selector: 'app-doc-viewer',
Expand All @@ -26,12 +44,12 @@ import { untilDestroyed } from 'ngx-take-until-destroy';
})
export class DocViewerComponent implements OnChanges, OnDestroy {

@Input() gqlSchema = null;
@Input() gqlSchema: GraphQLSchema
@Input() allowIntrospection = true;
@Input() isLoading = false;
@Input() addQueryDepthLimit = config.add_query_depth_limit;
@Input() tabSize = config.tab_size;
@Input() docView = {
@Input() docView: DocumentView = {
view: 'root', // type, field, root, search
parentType: 'Query', // used by field views
name: 'Conference' // identifies type/field
Expand All @@ -44,18 +62,18 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

@HostBinding('style.flex-grow') public resizeFactor;

rootTypes = [];
index = [];
rootTypes: GraphQLType[] = [];
index: DocumentIndexEntry[] = [];

searchInputPlaceholder = 'Search docs...';

// Used to determine if index related actions (like search, add query, etc.)
// should be available
hasSearchIndex = false;

docHistory = [];
docHistory: DocumentView[] = [];

searchResult = [];
searchResult: DocumentIndexEntry[] = [];
searchTerm = '';

constructor(
Expand Down Expand Up @@ -84,7 +102,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
schema.getQueryType(),
schema.getMutationType(),
schema.getSubscriptionType()
].filter(val => !!val);
].filter(Boolean);

try {
this.generateIndex(schema);
Expand All @@ -100,8 +118,8 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param schema
*/
generateIndex(schema) {
let getFieldsIndices = null;
let getTypeIndices = null;
let getFieldsIndices;
let getTypeIndices;

/**
* Gets the indices for fields
Expand All @@ -111,8 +129,8 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param {array} curIndexStack contains all the currently mapped indices in the stack
* @return {array} the indices for the given fields
*/
getFieldsIndices = (fields, type, isQuery, curIndexStack) => {
let index = [];
getFieldsIndices = (fields, type, isQuery: Boolean, curIndexStack) => {
let index: DocumentIndexEntry[] = [];

Object.keys(fields).forEach(fieldKey => {
const field = fields[fieldKey];
Expand All @@ -128,7 +146,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
isQuery,
highlight: 'field'
};
index = [...index, fieldIndex];
index = [ ...index, fieldIndex ];

// For each argument of the field, create an entry in the index for the field,
// searchable by the argument name
Expand Down Expand Up @@ -307,19 +325,23 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param parentType parent type of the current field
* @param parentFields preceding parent field and type combinations
*/
generateQuery(name, parentType): { query: String, meta: any } {
generateQuery(name, parentType) {
let query = '';
let hasArgs = false;

if (!this.gqlSchema) {
return;
}

// Add the root type of the query
switch (parentType) {
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType().name:
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType()!.name:
query += 'query';
break;
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType().name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType()!.name:
query += 'mutation';
break;
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType().name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType()!.name:
query += 'subscription';
break;
default:
Expand All @@ -329,10 +351,14 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

const fieldData = this.generateFieldData(name, parentType, [], 1);

if (!fieldData) {
return;
}

// Add the query fields
query += `{\n${fieldData.query}\n}`;

const meta = {...fieldData.meta};
const meta = { ...fieldData.meta };

// Update hasArgs option
meta.hasArgs = hasArgs || meta.hasArgs;
Expand All @@ -347,17 +373,24 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param parentFields preceding parent field and type combinations
* @param level current depth level of the current field
*/
private generateFieldData(name, parentType, parentFields, level): { query: String, meta: any } {
private generateFieldData(name, parentType, parentFields, level: number): { query: string, meta: { hasArgs?: boolean } } {

if (!name || !parentType || !parentFields) {
return { query: '', meta: {} };
}
const tabSize = this.tabSize || 2;
const field = this.gqlSchema.getType(parentType).getFields()[name];
const parentTypeObject = this.gqlSchema.getType(parentType) as GraphQLObjectType | undefined;
const field = parentTypeObject && parentTypeObject.getFields()[name];

if (!field) {
return { query: '', meta: {} };
}
const meta = {
hasArgs: false
};

// Start the query with the field name
let fieldStr: String = ' '.repeat(level * tabSize) + field.name;
let fieldStr: string = ' '.repeat(level * tabSize) + field.name;

// If the field has arguments, add them
if (field.args && field.args.length) {
Expand All @@ -372,7 +405,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

// Retrieve the current field type
const curTypeName = this.cleanName(field.type.inspect());
const curType = this.gqlSchema.getType(curTypeName);
const curType = this.gqlSchema.getType(curTypeName) as GraphQLObjectType | undefined;

// Don't add a field if it has been added in the query already.
// This happens when there is a recursive field
Expand All @@ -387,7 +420,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

// Get all the fields of the field type, if available
const innerFields = curType && curType.getFields && curType.getFields();
let innerFieldsData: String = null;
let innerFieldsData = '';
if (innerFields) {
innerFieldsData = Object.keys(innerFields).reduce((acc, cur) => {
// Don't add a field if it has been added in the query already.
Expand All @@ -397,10 +430,14 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
}

const curInnerFieldData = this.generateFieldData(cur, curTypeName, [...parentFields, { name, type: curTypeName }], level + 1);
if (!curInnerFieldData) {
return acc;
}

const curInnerFieldStr: String = curInnerFieldData.query;

// Set the hasArgs meta if the inner field has args
meta.hasArgs = meta.hasArgs || curInnerFieldData.meta.hasArgs;
meta.hasArgs = meta.hasArgs || curInnerFieldData.meta.hasArgs || false;

// Don't bother adding the field if there was nothing generated.
// This should fix the empty line issue in the inserted queries
Expand All @@ -426,7 +463,10 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
if (!this.hasSearchIndex) {
return false;
}
this.addQueryToEditorChange.next(this.generateQuery(name, parentType));
const generatedQuery = this.generateQuery(name, parentType);
if (generatedQuery) {
this.addQueryToEditorChange.next(generatedQuery);
}
}

exportSDL() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class EnvironmentManagerComponent implements OnInit {
};

selectedEnvironmentId = 'base';
selectedEnvironment: fromEnvironments.EnvironmentState = null;
selectedEnvironment?: fromEnvironments.EnvironmentState;
editorContent = '{}';
editorTitle = '';

Expand Down
8 changes: 4 additions & 4 deletions src/app/components/fancy-input/fancy-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {


highlightData = {
sections: []
sections: [] as any[]
};

private innerValue = '';
Expand Down Expand Up @@ -123,7 +123,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {
}
fixFirefox() {}
getRanges(val, highlight) {
const ranges = [];
const ranges: any[] = [];
let match;
while (match = highlight.exec(val), match !== null) {
ranges.push([match.index, match.index + match[0].length]);
Expand All @@ -138,7 +138,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {

// Prevent overlapping ranges
removeStaggeredRanges(ranges) {
const unstaggeredRanges = [];
const unstaggeredRanges: any[] = [];
ranges.forEach((range) => {
const isStaggered = unstaggeredRanges.some((unstaggeredRange) => {
const isStartInside = range[0] > unstaggeredRange[0] && range[0] < unstaggeredRange[1];
Expand Down Expand Up @@ -185,7 +185,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {
});
}
generateHighlightSections(val: string, boundaries: BoundaryMarker[]) {
const sections = [];
const sections: any[] = [];
let lastBoundary = {
index: 0,
type: ''
Expand Down

0 comments on commit 51ddcba

Please sign in to comment.