-
Notifications
You must be signed in to change notification settings - Fork 0
wip: create the documentation for the module 'forms' in flottform #54
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
Open
nidhal-labidi
wants to merge
11
commits into
main
Choose a base branch
from
feat/documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12cd23b
wip: create the documentation for the module 'forms' in flottform
nidhal-labidi 5b7e18f
wip: remove documentation from private methods
nidhal-labidi 6dc8c83
fix: remove the output of the script 'docs'
nidhal-labidi 92c7c62
Add .gitignore file to the module 'forms'
nidhal-labidi c90bbb8
Merge branch 'main' into feat/documentation
nidhal-labidi b7b84e0
fix: remove theme function since we have a new default ui
nidhal-labidi 55520df
Add documentation for default-component
nidhal-labidi e9f11f5
merge branch 'main' into 'feat/documentation'
nidhal-labidi 8eaab45
update documentation
nidhal-labidi 0090a4d
wip: update the sytles of the docs
nidhal-labidi ada6495
fix: change the colors and the font
nidhal-labidi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| .DS_Store | ||
| node_modules | ||
| /build | ||
| /.svelte-kit | ||
| /package | ||
| .env | ||
| .env.* | ||
| !.env.example | ||
| .vercel | ||
| .output | ||
| vite.config.js.timestamp-* | ||
| vite.config.ts.timestamp-* | ||
| /test-results | ||
| /docs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,8 +40,66 @@ const createLinkAndQrCode = (qrCode: string, link: string) => { | |||||
| createChannelLinkWithOffer | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * Creates and attaches a default Flottform UI component to the specified anchor element. This UI acts as an intermediary to facilitate WebRTC-based peer-to-peer connections between two devices, allowing one peer to send data (files or text) to the other. | ||||||
| * | ||||||
| * Developers can customize aspects of the UI, such as the button text, descriptions, and CSS classes, while using the default behavior provided by the function to quickly set up the peer-to-peer data transfer mechanism. | ||||||
| * | ||||||
| * The generated UI component will be attached as a child to `flottformAnchorElement`. The UI includes a dialog with a QR code or link that the second peer can use to connect and upload files/text to the main form. | ||||||
| * | ||||||
| * The dialog also shows the progress of receiving the file or text from the other device or any errors that can happen. | ||||||
| * | ||||||
| * The returned object allows for further interactions, such as adding the UI necessary to handle receiving a file or text, and retrieving all existing Flottform items within the UI. | ||||||
| * | ||||||
| * @param {Object} params - Configuration options for setting up the Flottform component. | ||||||
| * @param {HTMLElement} params.flottformAnchorElement - The HTML element to which the Flottform component will be attached. It determines where the UI will be built on the page. | ||||||
| * @param {string} [params.id] - Optional ID for the UI used for File of Text element. | ||||||
| * @param {HTMLElement} [params.flottformRootElement] - An optional root element to use. If not provided, a new default root element (a `div` with the class `flottform-root`) will be created. | ||||||
| * @param {string} [params.additionalComponentClass] - Optional additional class to add for custom styling of the component. | ||||||
| * @param {string} [params.flottformRootTitle] - Optional title to set for the Flottform root element This is the text displayed on the button that opens the dialog (with the class `flottform-root-opener-button`). | ||||||
| * @param {string} [params.flottformRootDescription] - Optional description text shown inside the dialog when it is opened. It provides context for the user about what the Flottform component does (e.g., "Receive files from other devices"). | ||||||
| * | ||||||
| * @returns {Object} - Returns an object with methods to interact with the Flottform component. | ||||||
| * @returns {HTMLElement} returns.flottformRoot - The root element of the Flottform UI. | ||||||
| * @returns {Function} returns.createFileItem - Function to create an entry in the UI for receiving files from another peer. The entry will show the QR code/link, progress of the file transfer, and handle any errors. | ||||||
| * @returns {Function} returns.createTextItem - Function to create an entry in the UI for receiving text from another peer. Similar to `createFileItem`, it handles text input, progress tracking, and error handling. | ||||||
| * @returns {Function} returns.getAllFlottformItems - Function to retrieve all current Flottform items (file and text entries) in the dialog. | ||||||
| * | ||||||
| * @example | ||||||
| * | ||||||
| * | ||||||
| * const flottformComponent = createDefaultFlottformComponent({ | ||||||
| * flottformAnchorElement: document.getElementById('form-anchor'), | ||||||
| * flottformRootTitle: 'Share Data via Flottform', | ||||||
| * flottformRootDescription: 'This form is powered by Flottform. Upload files or send text from another device using the provided QR code.' | ||||||
| *}); | ||||||
| * | ||||||
| * // Create an entry to receive files | ||||||
| * flottformComponent.createFileItem({ | ||||||
| * flottformApi, // URL of the WebRTC signaling server | ||||||
| * createClientUrl: ({ endpointId }) => `/upload/${endpointId}`, // URL of the client page for file uploads | ||||||
| * inputField: document.querySelector('#fileInput'), // The file input field in the main form | ||||||
| * label: 'Upload Resume', // Label for the file input | ||||||
| * buttonLabel:'Submit File', // Button text for the file input | ||||||
| * onSuccessText: 'File received successfully!' // Success message displayed after the file is received | ||||||
| * }); | ||||||
| * | ||||||
| * // Create an entry to receive text | ||||||
| * flottformComponent.createTextItem({ | ||||||
| * flottformApi, // URL of the WebRTC signaling server | ||||||
| * createClientUrl: ({ endpointId }) => `/text/${endpointId}`, // URL of the client page for sending text | ||||||
| * label: 'Enter your message', // Label for the text input | ||||||
| * buttonLabel: 'Send Message', // Button text for the text input | ||||||
| * onErrorText: (error) => `Failed to receive text: ${error.message}` // Error message if text transfer fails | ||||||
| * }); | ||||||
| * | ||||||
| * // Retrieve all Flottform items in the UI (file and text entries) | ||||||
| * const allItems = flottformComponent.getAllFlottformItems(); | ||||||
| */ | ||||||
| export const createDefaultFlottformComponent = ({ | ||||||
| flottformAnchorElement, | ||||||
| // @ts-ignore: Unused variable | ||||||
| id, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should remove this completely
Suggested change
|
||||||
| flottformRootElement, | ||||||
| additionalComponentClass, | ||||||
|
|
@@ -74,6 +132,11 @@ export const createDefaultFlottformComponent = ({ | |||||
| flottformAnchorElement.appendChild(flottformRoot); | ||||||
| return { | ||||||
| flottformRoot, | ||||||
| /** | ||||||
| * Retrieves all existing Flottform items (both file and text entries) in the UI. | ||||||
| * | ||||||
| * @returns {NodeListOf<Element> | null} - A NodeList of Flottform input items (file and text entries), or `null` if no items are found. | ||||||
| */ | ||||||
| getAllFlottformItems: () => { | ||||||
| const flottformInputsList = flottformRoot.querySelector('.flottform-inputs-list'); | ||||||
| if (!flottformInputsList) { | ||||||
|
|
@@ -82,6 +145,22 @@ export const createDefaultFlottformComponent = ({ | |||||
| } | ||||||
| return flottformInputsList.childNodes as NodeListOf<Element>; | ||||||
| }, | ||||||
| /** | ||||||
| * Creates a UI entry for receiving a file via WebRTC. | ||||||
| * | ||||||
| * @param {Object} params - Configuration options for the file input entry. | ||||||
| * @param {string} params.flottformApi - URL of the WebRTC signaling server. | ||||||
| * @param {Function} params.createClientUrl - A function that returns the URL where the second peer can upload the file. | ||||||
| * @param {HTMLInputElement} params.inputField - The file input field in the main form where the received file will be displayed or processed. | ||||||
| * @param {string} [params.id] - Optional ID for the file input entry. | ||||||
| * @param {string} [params.additionalItemClasses] - Optional additional CSS classes for styling the file input entry. | ||||||
| * @param {string} [params.label] - Optional label text for the file input entry. | ||||||
| * @param {string} [params.buttonLabel] - Optional text for the button that triggers the file reception. | ||||||
| * @param {string | Function} [params.onErrorText] - Optional error message displayed if the file transfer fails. | ||||||
| * @param {string} [params.onSuccessText] - Optional success message displayed after the file is successfully received. | ||||||
| * | ||||||
| * @returns {void} | ||||||
| */ | ||||||
| createFileItem: ({ | ||||||
| flottformApi, | ||||||
| createClientUrl, | ||||||
|
|
@@ -137,6 +216,21 @@ export const createDefaultFlottformComponent = ({ | |||||
| onSuccessText | ||||||
| }); | ||||||
| }, | ||||||
| /** | ||||||
| * Creates a UI entry for receiving text via WebRTC. | ||||||
| * | ||||||
| * @param {Object} params - Configuration options for the text input entry. | ||||||
| * @param {string} params.flottformApi - URL of the WebRTC signaling server. | ||||||
| * @param {Function} params.createClientUrl - A function that returns the URL where the second peer can send the text. | ||||||
| * @param {string} [params.id] - Optional ID for the text input entry. | ||||||
| * @param {string} [params.additionalItemClasses] - Optional additional CSS classes for styling the text input entry. | ||||||
| * @param {string} [params.label] - Optional label text for the text input entry. | ||||||
| * @param {string} [params.buttonLabel] - Optional text for the button that triggers the text reception. | ||||||
| * @param {string | Function} [params.onErrorText] - Optional error message displayed if the text transfer fails. | ||||||
| * @param {string} [params.onSuccessText] - Optional success message displayed after the text is successfully received. | ||||||
| * | ||||||
| * @returns {void} | ||||||
| */ | ||||||
| createTextItem: ({ | ||||||
| flottformApi, | ||||||
| createClientUrl, | ||||||
|
|
@@ -227,7 +321,9 @@ const createBaseFlottformItems = <L extends BaseListeners>({ | |||||
| }); | ||||||
| flottformBaseInputHost.on('error', (error) => { | ||||||
| statusInformation.innerHTML = | ||||||
| typeof onErrorText === "function" ? onErrorText(error) : (onErrorText ?? `🚨 An error occured (${error.message}). Please try again`); | ||||||
| typeof onErrorText === 'function' | ||||||
| ? onErrorText(error) | ||||||
| : onErrorText ?? `🚨 An error occured (${error.message}). Please try again`; | ||||||
| createChannelButton.innerText = 'Retry'; | ||||||
| flottformStateItemsContainer.replaceChildren(statusInformation); | ||||||
| flottformStateItemsContainer.appendChild(createChannelButton); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,26 @@ type Listeners = { | |
| error: [e: string]; | ||
| bufferedamountlow: []; | ||
| }; | ||
|
|
||
| /** | ||
| * A class used to represent one peer (called client) to establish a WebRTC connection with another peer (called host). | ||
| * It handles ICE candidate gathering and sending/receiving data. | ||
| * The connection is initiated only when `start` method is called. | ||
| * | ||
| * This class emits various events during the connection lifecycle, such as `connected`, `disconnected`, and `error`, allowing you to respond to changes in the connection state. | ||
| * | ||
| * @fires init - Emitted when the client is initialized. | ||
| * @fires retrieving-info-from-endpoint - Emitted when information is being retrieved from the endpoint. | ||
| * @fires sending-client-info - Emitted when client information is being sent to the host. | ||
| * @fires connecting-to-host - Emitted when attempting to connect to the host. | ||
| * @fires connected - Emitted when the connection is successfully established. | ||
| * @fires connection-impossible - Emitted if the connection to the host cannot be established. | ||
| * @fires done - Emitted when the all of the data is received. | ||
| * @fires disconnected - Emitted when the connection is closed. | ||
| * @fires error - Emitted when there is an error during the connection. | ||
| * @fires bufferedamountlow - Emitted when the buffered amount for data channels is low. | ||
| * | ||
| * @extends EventEmitter | ||
| */ | ||
| export class FlottformChannelClient extends EventEmitter<Listeners> { | ||
| private flottformApi: string; | ||
| private endpointId: string; | ||
|
|
@@ -34,7 +53,17 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| private dataChannel: RTCDataChannel | null = null; | ||
| private pollForIceTimer: NodeJS.Timeout | number | null = null; | ||
| private BUFFER_THRESHOLD = 128 * 1024; // 128KB buffer threshold (maximum of 4 chunks in the buffer waiting to be sent over the network) | ||
|
|
||
| /** | ||
| * Creates an instance of FlottformChannelClient | ||
| * | ||
| * @param {Object} config - The configuration for setting up the channel for the host. | ||
| * | ||
| * @param {endpointId} - The unique identifier of the endpoint to connect to. | ||
| * @param {flottformApi} - The API endpoint for retrieving connection information. | ||
| * @param {rtcConfiguration} - Optional RTC configuration for WebRTC connection. | ||
| * @param {pollTimeForIceInMs} - Optional time in milliseconds for polling ICE candidates. | ||
| * @param {logger} - Optional logger for logging connection events (default: `console`). | ||
| */ | ||
| constructor({ | ||
| endpointId, | ||
| flottformApi, | ||
|
|
@@ -62,6 +91,10 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| this.logger.info(`**Client State changed to: ${newState}`, details == undefined ? '' : details); | ||
| }; | ||
|
|
||
| /** | ||
| * Starts the WebRTC connection process. The connection is not established until this method is called. | ||
| * | ||
| */ | ||
| start = async () => { | ||
| if (this.openPeerConnection) { | ||
| this.close(); | ||
|
|
@@ -102,6 +135,11 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| this.startPollingForIceCandidates(getEndpointInfoUrl); | ||
| }; | ||
|
|
||
| /** | ||
| * Closes the WebRTC connection if it is currently established. | ||
| * | ||
| * @fires disconnected - Emitted when the connection is successfully closed. | ||
| */ | ||
| close = () => { | ||
| if (this.openPeerConnection) { | ||
| this.openPeerConnection.close(); | ||
|
|
@@ -110,6 +148,12 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| this.changeState('disconnected'); | ||
| }; | ||
|
|
||
| /** | ||
| * Sends data to the connected peer via the WebRTC data channel. | ||
| * | ||
| * @param data - The data to send to the peer. | ||
| * @fires error - Emits the state error if the connection is not established. | ||
| */ | ||
| sendData = (data: any) => { | ||
| if (this.dataChannel == null) { | ||
| this.changeState('error', 'dataChannel is null. Unable to send the file to the Host!'); | ||
|
|
@@ -121,6 +165,11 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| this.dataChannel.send(data); | ||
| }; | ||
|
|
||
| /** | ||
| * Determines if more data can be sent based on the WebRTC data channel's buffered amount. This is useful when dealing with large amounts of data. | ||
| * | ||
| * @returns `true` if more data can be sent, otherwise `false`. | ||
| */ | ||
| canSendMoreData = () => { | ||
| return ( | ||
| this.dataChannel && | ||
|
|
@@ -154,13 +203,15 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| } | ||
| } | ||
| }; | ||
| // @ts-ignore: Unused variable | ||
| this.openPeerConnection.onicegatheringstatechange = async (e) => { | ||
| this.logger.info(`onicegatheringstatechange - ${this.openPeerConnection!.iceGatheringState}`); | ||
| }; | ||
| this.openPeerConnection.onicecandidateerror = (e) => { | ||
| this.logger.error(`onicecandidateerror - ${this.openPeerConnection!.connectionState}`, e); | ||
| }; | ||
| }; | ||
|
|
||
| private setUpConnectionStateGathering = (getEndpointInfoUrl: string) => { | ||
| if (this.openPeerConnection === null) { | ||
| this.changeState( | ||
|
|
@@ -187,7 +238,7 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| } | ||
| } | ||
| }; | ||
|
|
||
| // @ts-ignore: Unused variable | ||
| this.openPeerConnection.oniceconnectionstatechange = (e) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we read the |
||
| this.logger.info( | ||
| `oniceconnectionstatechange - ${this.openPeerConnection!.iceConnectionState}` | ||
|
|
@@ -198,12 +249,14 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| } | ||
| }; | ||
| }; | ||
|
|
||
| private stopPollingForIceCandidates = async () => { | ||
| if (this.pollForIceTimer) { | ||
| clearTimeout(this.pollForIceTimer); | ||
| } | ||
| this.pollForIceTimer = null; | ||
| }; | ||
|
|
||
| private startPollingForIceCandidates = async (getEndpointInfoUrl: string) => { | ||
| if (this.pollForIceTimer) { | ||
| clearTimeout(this.pollForIceTimer); | ||
|
|
@@ -213,6 +266,7 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
|
|
||
| this.pollForIceTimer = setTimeout(this.startPollingForIceCandidates, this.pollTimeForIceInMs); | ||
| }; | ||
|
|
||
| private pollForConnection = async (getEndpointInfoUrl: string) => { | ||
| if (this.openPeerConnection === null) { | ||
| this.changeState('error', "openPeerConnection is null. Unable to retrieve Host's details"); | ||
|
|
@@ -225,6 +279,7 @@ export class FlottformChannelClient extends EventEmitter<Listeners> { | |
| await this.openPeerConnection.addIceCandidate(iceCandidate); | ||
| } | ||
| }; | ||
|
|
||
| private putClientInfo = async ( | ||
| putClientInfoUrl: string, | ||
| clientKey: string, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be removed here. It's in the
createFileInputandcreateTextInputalready, so it's never used.