start working on advanced selection tool...
This commit is contained in:
parent
909b048468
commit
5c832cb94b
1 changed files with 68 additions and 8 deletions
|
|
@ -426,6 +426,29 @@ function selectContentOnMouseUp(event) {
|
||||||
// If no dragging has occurred, execute the selection code
|
// If no dragging has occurred, execute the selection code
|
||||||
selectContentOnClick(event);
|
selectContentOnClick(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Capture the text selection and insert markers
|
||||||
|
const selection = window.getSelection();
|
||||||
|
|
||||||
|
if (!selection.isCollapsed) {
|
||||||
|
const range = selection.getRangeAt(0);
|
||||||
|
|
||||||
|
// Create and insert the markers
|
||||||
|
const startMarker = createMarker('start-marker');
|
||||||
|
const endMarker = createMarker('end-marker');
|
||||||
|
|
||||||
|
range.insertNode(startMarker);
|
||||||
|
|
||||||
|
//selection.removeAllRanges();
|
||||||
|
range.setStartAfter(startMarker);
|
||||||
|
|
||||||
|
range.collapse(false);
|
||||||
|
range.insertNode(endMarker);
|
||||||
|
|
||||||
|
//selection.removeAllRanges();
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the event listeners to prevent future executions
|
// Remove the event listeners to prevent future executions
|
||||||
// document.removeEventListener('mousedown', selectContentOnMouseDown);
|
// document.removeEventListener('mousedown', selectContentOnMouseDown);
|
||||||
// document.removeEventListener('mousemove', selectContentOnMouseMove);
|
// document.removeEventListener('mousemove', selectContentOnMouseMove);
|
||||||
|
|
@ -436,34 +459,71 @@ function selectContentOnClick(event) {
|
||||||
if(current_action === '0'){
|
if(current_action === '0'){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent the default behavior of the click
|
// Prevent the default behavior of the click
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// Get the element that was clicked
|
// Get the element that was clicked
|
||||||
var clickedElement = event.target;
|
var clickedElement = event.target;
|
||||||
|
|
||||||
// Traverse the DOM upwards to find the nearest parent div
|
// Traverse the DOM upwards to find the nearest parent div
|
||||||
var parentDiv = clickedElement.closest('div');
|
var parentDiv = clickedElement.closest('div');
|
||||||
|
|
||||||
if (parentDiv) {
|
if (parentDiv) {
|
||||||
// Create a range object
|
// Create a range object
|
||||||
var range = document.createRange();
|
var range = document.createRange();
|
||||||
|
|
||||||
// Select the contents of the div
|
// Select the contents of the div
|
||||||
range.selectNodeContents(parentDiv);
|
range.selectNodeContents(parentDiv);
|
||||||
|
|
||||||
// Get the selection object
|
// Get the selection object
|
||||||
var selection = window.getSelection();
|
var selection = window.getSelection();
|
||||||
|
|
||||||
// Clear any existing selections
|
// Clear any existing selections
|
||||||
selection.removeAllRanges();
|
selection.removeAllRanges();
|
||||||
|
|
||||||
// Add the new range to the selection
|
// Add the new range to the selection
|
||||||
selection.addRange(range);
|
selection.addRange(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to create a marker with draggable attribute
|
||||||
|
function createMarker(id) {
|
||||||
|
const marker = document.createElement('span');
|
||||||
|
marker.id = id;
|
||||||
|
marker.textContent = '||||';
|
||||||
|
marker.draggable = true;
|
||||||
|
marker.style.cursor = 'move';
|
||||||
|
|
||||||
|
marker.addEventListener('dragstart', onDragStart);
|
||||||
|
marker.addEventListener('dragover', onDragOver);
|
||||||
|
marker.addEventListener('drop', onDrop);
|
||||||
|
marker.addEventListener('dragend', onDragEnd);
|
||||||
|
|
||||||
|
return marker;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function called at the start of the drag
|
||||||
|
function onDragStart(event) {
|
||||||
|
event.dataTransfer.setData('text/plain', event.target.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow dragover (necessary to enable drop)
|
||||||
|
function onDragOver(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function called at the time of the drop
|
||||||
|
function onDrop(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const id = event.dataTransfer.getData('text/plain');
|
||||||
|
const draggedElement = document.getElementById(id);
|
||||||
|
|
||||||
|
if (draggedElement) {
|
||||||
|
const range = document.caretRangeFromPoint(event.clientX, event.clientY);
|
||||||
|
range.insertNode(draggedElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function called at the end of the drag (can be used for cleanup or other actions)
|
||||||
|
function onDragEnd(event) {
|
||||||
|
// Optional actions at the end of the drag, e.g., removing any remaining selections
|
||||||
|
window.getSelection().removeAllRanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// In the content script
|
// In the content script
|
||||||
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue