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

Fix Color Picker Position #664

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
81 changes: 48 additions & 33 deletions src/client/CodeEditor/InteractiveWidgets/colorPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,54 @@ export const colorPickerRegex = /#[0-9A-Fa-f]{6}/g;
// Works without quotes to support CSS.
export const colorPicker = (
onInteract?: () => void,
): InteractRule => ({
regexp: colorPickerRegex,
cursor: 'pointer',
onClick(
text: string,
setText: (newText: string) => void,
) {
const startingColor: string = text;

const sel: HTMLInputElement =
document.createElement('input');
sel.type = 'color';
sel.value = startingColor.toLowerCase();

// `valueIsUpper` maintains the style of the user's code.
// It keeps the case of a-f the same case as the original.
const valueIsUpper: boolean =
startingColor.toUpperCase() === startingColor;

const updateHex = (e: Event) => {
const el: HTMLInputElement =
e.target as HTMLInputElement;
if (el.value) {
setText(
valueIsUpper ? el.value.toUpperCase() : el.value,
);
}
if (onInteract) onInteract();
};
sel.addEventListener('input', updateHex);
sel.click();
},
});
): InteractRule => {
// Create the color picker element when the page loads
const sel: HTMLInputElement = document.createElement('input');
sel.type = 'color';
sel.style.position = 'fixed';
sel.style.left = '-9999px'; // Position off-screen initially
sel.style.top = '-9999px';
document.body.appendChild(sel);

return {
regexp: colorPickerRegex,
cursor: 'pointer',

onClick(
text: string,
setText: (newText: string) => void,
event: MouseEvent // Capture the event object
) {
const startingColor: string = text;

// Set the initial value of the color picker
sel.value = startingColor.toLowerCase();

// Calculate the position of the cursor within the document
const cursorX = event.clientX;
const cursorY = event.clientY;

// Position the color picker relative to the cursor position
sel.style.left = cursorX + 'px';
sel.style.top = cursorY + 'px';

const updateHex = (e: Event) => {
const el: HTMLInputElement =
e.target as HTMLInputElement;
if (el.value) {
setText(el.value.toUpperCase()); // Always use upper case
}
if (onInteract) onInteract();
};

// Add input event listener to handle color selection
sel.addEventListener('input', updateHex);

// Trigger click event on the color picker to display it
sel.click();
},
};
};

// TODO get this working
// rgb color picker
Expand Down
6 changes: 3 additions & 3 deletions test/sampleDirectories/kitchenSink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const num = 451;
let cyberData = {};
cyberData['test'] = 'foo';
const initialData = [
{ x: 155, y: 386, r: 20, fill: '#5B9B31' },
{ x: 155, y: 386#789763, fill: '#5B9B31' },
{ x: 340, y: 238, r: 52, fill: '#99156D' },
{ x: 531, y: 151, r: 20, fill: '#00FF88' },
{ x: 531, y: 1#29387520, fill: '#00FF88' },
{ x: 482, y: 307, r: 147, fill: '#55AA95' },
{ x: 781, y: 91, r: 61, fill: '#0E3DFB' },
{ x: 781, y#293875: 61, fill: '#0E3DFB' },
{ x: 668, y: 229, r: 64, fill: '#7300FF' },
// TODO add more circles
{ x: 447, y: 350, r: 70, fill: '#007AFF' },
Expand Down