Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-React view option for external-views #3122

Merged
merged 5 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions examples/hosts/app-integration/external-views/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
"@fluidframework/aqueduct": "^0.25.0",
"@fluidframework/container-loader": "^0.25.0",
"@fluidframework/get-tinylicious-container": "^0.25.0",
"@fluidframework/map": "^0.25.0",
"react": "^16.10.2",
"react-dom": "^16.10.2"
"@fluidframework/map": "^0.25.0"
},
"devDependencies": {
"@fluidframework/build-common": "^0.18.0",
Expand All @@ -47,8 +45,6 @@
"@types/jest-environment-puppeteer": "2.2.0",
"@types/node": "^10.17.24",
"@types/puppeteer": "1.3.0",
"@types/react": "^16.9.15",
"@types/react-dom": "^16.9.4",
"@typescript-eslint/eslint-plugin": "~2.17.0",
"@typescript-eslint/parser": "~2.17.0",
"clean-webpack-plugin": "^3.0.0",
Expand Down
16 changes: 6 additions & 10 deletions examples/hosts/app-integration/external-views/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import { Container } from "@fluidframework/container-loader";
import { getTinyliciousContainer } from "@fluidframework/get-tinylicious-container";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { IDiceRoller } from "../component";
import { DiceRollerContainerRuntimeFactory } from "../container";
import { PrettyDiceRollerView } from "./views";
import { renderDiceRoller } from "./views";

// I'm choosing to put the docId in the hash just for my own convenience, so the URL will end up looking something
// like http://localhost:8080/#1596520748752. This is not crucial to the scenario -- there should be no requirements
Expand Down Expand Up @@ -41,18 +39,16 @@ async function getDiceRollerFromContainer(container: Container): Promise<IDiceRo
return response.value;
}

// Given an IDiceRoller, we can render its data using the PrettyDiceRollerView we've created in our app.
async function renderPrettyDiceRoller(diceRoller: IDiceRoller) {
const div = document.getElementById("content") as HTMLDivElement;
ReactDOM.render(React.createElement(PrettyDiceRollerView, { model: diceRoller }), div);
}

// Just a helper function to kick things off. Making it async allows us to use await.
async function start(): Promise<void> {
// Get the container to use. Associate the data with the provided documentId, and run the provided code within.
const container = await getTinyliciousContainer(documentId, DiceRollerContainerRuntimeFactory, createNew);
const diceRoller = await getDiceRollerFromContainer(container);
await renderPrettyDiceRoller(diceRoller);

// Given an IDiceRoller, we can render its data using the view we've created in our app.
const div = document.getElementById("content") as HTMLDivElement;
renderDiceRoller(diceRoller, div);

// Setting "fluidStarted" is just for our test automation
// eslint-disable-next-line dot-notation
window["fluidStarted"] = true;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* Licensed under the MIT License.
*/

export * from "./PrettyDiceRollerView";
export * from "./renderDiceRoller";
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { IDiceRoller } from "../../component";

/**
* Render an IDiceRoller into a given div as a text character, with a button to roll it.
* @param diceRoller - The Data Object to be rendered
* @param div - The div to render into
*/
export function renderDiceRoller(diceRoller: IDiceRoller, div: HTMLDivElement) {
const wrapperDiv = document.createElement("div");
wrapperDiv.style.textAlign = "center";
div.append(wrapperDiv);

const diceCharDiv = document.createElement("div");
diceCharDiv.style.fontSize = "200px";

const rollButton = document.createElement("button");
rollButton.style.fontSize = "50px";
rollButton.textContent = "Roll";
// Call the roll method to modify the shared data when the button is clicked
rollButton.addEventListener("click", diceRoller.roll);

wrapperDiv.append(diceCharDiv, rollButton);

// Get the current value of the shared data to update the view whenever it changes.
const updateDiceChar = () => {
// Unicode 0x2680-0x2685 are the sides of a dice (⚀⚁⚂⚃⚄⚅)
diceCharDiv.textContent = String.fromCodePoint(0x267F + diceRoller.value);
diceCharDiv.style.color = `hsl(${diceRoller.value * 60}, 70%, 50%)`;
};
updateDiceChar();

// Use the diceRolled event to trigger the rerender whenever the value changes.
diceRoller.on("diceRolled", updateDiceChar);
}