Etherpad: How to capture selected text from etherpad - etherpad

I want to capture selected text from Etherpad. There is API methods available /getText that will return the entire text.
My requirement is to get only selected text.
Thanks in advance!

If you want to use the selected text in an outer frame, you could use a postMessage System.
exports.postAceInit = (hookName, context) => {
window.addEventListener('message', function receiver(e) {
// Data of Request
let data = e.data;
// Origin URL of Request
let origin = e.origin;
if(data == 'GET_SELECTION'){
context.ace.callWithAce((ace) => {
// Read current selection
let rep = ace.ace_getRep();
// Start of the Selection [x,y]
let selStart = rep.selStart;
// End of the Selection [x,y]
let selEnd = rep.selEnd;
// Read Lines of Pad
let lines = rep.lines;
let retVal = '';
// Run through Selection
for(let idx = selStart[0]; idx < selEnd[0]+1; idx++){
retVal = retVal + lines.atIndex(idx).text;
}
// Send text to Receiver
e.source.postMessage(retVal, origin);
}, 'GET_SELECTION', true);
}
, false);
}
Receiver and Sender Site:
<script type="text/javascript">
function sendMessage(message) {
document.getElementById('etherpad').contentWindow.postMessage(message, '_URL OF YOUR ETHERPAD INSTANCE_');
}
function receiver(e) {
// Read Data of Request
let data = e.data;
// Origin URL of Request
let origin = e.origin;
alert("GOT: " + data + "\nFROM: " + origin);
e.source.postMessage('Thanks', origin);
}
// Listener gets Requests
window.addEventListener('message', receiver, false);
</script>
<button type="button" onclick="sendMessage('GET_SELECTION')">GET SELECTION</button>
This code will get you the whole lines of text from the selection. If you want to get the exacts characters, you would need a second loop within the idx for-loop

Related

Where should I use ParseFloat or Number functions in my code to change my geojson data?

Editing for specificity
The data in my geojson is formatted as string variables and I need to change those fields to numbers so my legend on my choropleth will read the data. I'm struggling to figure out where to use either the ParseFloat or Number functions so my leaflet map reads the data as a number.
I'm attempting to map each feature using different buttons. One button to show Population, another to show Average Water Bill, etc. I want the legend to show the data called on for each button. Below is the code I have for the legend and for the buttons...
// Global variables
let map;
let lat = 34;
let lon = -118;
let zl = 9;
let geojsonPath = 'https://raw.githubusercontent.com/LCIWaterProjects/DRAFT-LA-County-Governance-Map/main/data/Data_Update.geojson';
let geojson_data;
let geojson_layer;
let brew = new classyBrew();
let legend = L.control({position: 'bottomright'});
let info_panel = L.control();
let fieldtomap;
// initialize+
$( document ).ready(function() {
createMap(lat,lon,zl);
getGeoJSON();
});
// create the map
function createMap(lat,lon,zl){
map = L.map('map').setView([lat,lon], zl);
L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
{
attribution: 'Map data © OpenStreetMap contributors, Imagery © Mapbox',
maxZoom: 18,
id: 'light-v10',
tileSize: 512,
zoomOffset: -1,
accessToken: 'pk.eyJ1Ijoic2FyYWhwZXJlejEiLCJhIjoiY2t0MG9hZDNnMDZ2NDJ1c2M5dzBmb201OSJ9.5fv8NqX5cfA0NMcmEW_63g'
}).addTo(map);
}
// function to get the geojson data
$.getJSON(geojsonPath,function(data){
console.log(data)
// put the data in a global variable
geojson_data = data;
// call the map function
mapGeoJSON();
})
function mapGeoJSON(field,num_classes,color,scheme){
// clear layers in case it has been mapped already
if (geojson_layer){
geojson_layer.clearLayers()
}
// globalize the field to map
fieldtomap = field;
...
function createLegend(){
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
breaks = brew.getBreaks(),
labels = [],
from, to;
for (var i = 0; i < breaks.length; i++) {
from = breaks[i];
to = breaks[i + 1];
if(to) {
labels.push(
'<i style="background:' + brew.getColorInRange(to) + '"></i> ' +
from.toFixed(0) + ' – ' + to.toFixed(0));
}
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
// create buttons function
function myPopFunction(){
mapGeoJSON('Population',5,'YlOrRd','jenks');}
function myServeFunction(){
mapGeoJSON('Service_Co',5,'BuPu','quantiles');}

Prevent nested lists in text-editor (froala)

I need to prevent/disable nested lists in text editor implemented in Angular. So far i wrote a hack that undos a nested list when created by the user. But if the user creates a normal list and presses the tab-key the list is shown as nested for a few milliseconds until my hack sets in back to a normal list. I need something like event.preventDefault() or stopPropagation() on tab-event keydown but unfortunately that event is not tracked for some reason. Also the froala settings with tabSpaces: falseis not showing any difference when it comes to nested list...in summary i want is: if the user creates a list and presses the tab-key that nothing happens, not even for a millisecond. Has anyone an idea about that?
Froala’s support told us, there’s no built-in way to suppress nested list creation. They result from TAB key getting hit with the caret on a list item. However we found a way to get around this using MutationObserver
Basically we move the now nested list item to his former sibling and remove the newly created list. Finally we take care of the caret position.
var observer = new MutationObserver(mutationObserverCallback);
observer.observe(editorNode, {
childList: true,
subtree: true
});
var mutationObserverCallback = function (mutationList) {
var setCaret = function (ele) {
if (ele.nextSibling) {
ele = ele.nextSibling;
}
var range = document.createRange();
var sel = window.getSelection();
range.setStart(ele, 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
};
var handleAddedListNode = function (listNode) {
if (! listNode.parentNode) {
return;
}
var parentListItem = listNode.parentNode.closest('li');
if (!parentListItem) {
return;
}
var idx = listNode.children.length - 1;
while (idx >= 0) {
var childNode = listNode.children[idx];
if (parentListItem.nextSibling) {
parentListItem.parentNode.insertBefore(childNode, parentListItem.nextSibling);
} else {
parentListItem.parentNode.appendChild(childNode);
}
--idx;
}
setCaret(parentListItem);
listNode.parentNode.removeChild(listNode);
};
mutationList.forEach(function (mutation) {
var addedNodes = mutation.addedNodes;
if (!addedNodes.length) {
return;
}
for (var i = 0; i < addedNodes.length; i++) {
var currentNode = addedNodes[i];
switch (currentNode.nodeName.toLowerCase()) {
case 'ol':
case 'ul':
handleAddedListNode(currentNode);
break;
// more optimizations
}
}
})
};

I found a memory leak but couldn't understand it in swift 3

I found the following issue in my memory. I couldn't understand it.
error evaluating expression “(CAListenerProxy::DeviceAggregateNotification *)0x7cee3d60”: error: use of undeclared identifier 'CAListenerProxy'
error: expected expression
I used two notification centers - one for sending the object to another view and the other in another view to send the dictionary which contain objects after deleting one from it.
My code is :
// only for delegate method for the downloading videos
extension WebViewController {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
// her i need to get the data from the movie which i download it so i can save it in the document directory
if let fetchDataFromDownloadFile = try? Data(contentsOf: location) {
// generate the fileName randamlly
let createFileName = UUID().uuidString
// generate object for save file
let operationDocumentDirectory = OperationDocumentDirectory()
operationDocumentDirectory.saveMovie(movieName: createFileName, data: fetchDataFromDownloadFile)
}// end the if let for the fetch data from download file
// her for fetch the download video object when i save it to set it to ni for free the memory
if let fetchURL = downloadTask.originalRequest?.url {
var fetchObject = operationObject?.dictionaryOfDownloadVideo?.removeValue(forKey: fetchURL)
// for stop the downloadtask when finish download
if fetchObject?.videoURL == downloadTask.originalRequest?.url {
downloadTask.cancel()
fetchObject = nil
}
// for update the badge after finish download movie
DispatchQueue.main.async {[weak self] in
if let mySelf = self {
// set badge for nil if the objects zero
if operationObject.dictionaryOfDownloadVideo?.count == 0 {
self?.tabBarController?.viewControllers?[1].tabBarItem.badgeValue = nil
}else{
// if the object not zero update the badge
mySelf.tabBarController?.viewControllers?[1].tabBarItem.badgeValue = "\(operationObject.dictionaryOfDownloadVideo!.count)"
}
}// end the if for myself
}// end the dispatchqueue.main
// update the data in table view
NotificationCenter.default.post(name: NOTIFICATION_UPDATE_TABLEVIEW, object: nil)
}// end the fetch url
}
// and this code in another view for updating table view when i currently downloading movie
extension MovieDownloadingViewController {
// data source
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DownloadingTableViewCell
if let cell = cell {
cell.movieObject = arrayOfObjects?[indexPath.row]
cell.movieDeleteButton.tag = indexPath.row
cell.movieDeleteButton.addTarget(self, action: #selector(self.deleteCurrentDownloadingMovie(sender:)), for: .touchUpInside)
}
return cell!
}
func deleteCurrentDownloadingMovie(sender:UIButton){
displayAlertDeleteMovie(arrayOfObject: arrayOfObjects!, index: sender.tag)
}
func displayAlertDeleteMovie(arrayOfObject:[DownloadVideo],index:Int) {
let alertController = UIAlertController(title: "Delete Movie", message: "Do You Want Delete Movie \(arrayOfObject[index].videoName)", preferredStyle: .actionSheet)
let alertDelete = UIAlertAction(title: "Delete", style: .default) {[weak self] (alertAction:UIAlertAction) in
var fetchObjectMovie = self?.arrayOfObjects?.remove(at: index)
// set the notification for update the number of element in dict and array
NotificationCenter.default.post(name: NOTIFICATION_UPDATE_NUMBER_OF_ARRAY_DICT, object: fetchObjectMovie?.videoURL)
if fetchObjectMovie != nil {
fetchObjectMovie = nil
}
// update table view
// self?.tableView.reloadData()
// update the badge in the tab bar controller
if operationObject.dictionaryOfDownloadVideo?.count == 0 {
self?.tabBarController?.viewControllers?[1].tabBarItem.badgeValue = nil
}else{
self?.tabBarController?.viewControllers?[1].tabBarItem.badgeValue = "\(operationObject.dictionaryOfDownloadVideo!.count)"
}
}
let alertCancel = UIAlertAction(title: "Cancel", style: .cancel) { [weak self](alertAction:UIAlertAction) in
self?.dismiss(animated: true, completion: {})
}
alertController.addAction(alertDelete)
alertController.addAction(alertCancel)
present(alertController, animated: true, completion: nil)
}
please any help
thanks a lot

Need if-else advice in actionscript3

function clickButtonHandler(event:MouseEvent):void
{
var message:Object = new Object();
message.text = txtMessage.text;
message.userName = txtUser.text;
//Posts to this swf
showMessage(message);
//Posts to ALL OTHER swf files..
group.post(message);
}
function showMessage(message:Object):void
{
output_txt.appendText(message.userName+": "+message.text + "\n");
}
function jsalertwindow(event:MouseEvent):void
{
var alert:URLRequest = new URLRequest("javascript:alert('Please enter your User name')");
navigateToURL(alert, "_self");
}
As you can see there are two function which are contain mouseevent. I want to send those function with an if-else statement. If user write something in text input component which name is txtUser and,
send_btn.addEventListener(MouseEvent.CLICK, clickButtonHandler);
will work, else(if the user forget writing anything)
send_btn.addEventListener(MouseEvent.CLICK, jsalertwindow);
will work.
And one more question should i use MouseEvent.CLICK or MouseEvent.MOUSE_DOWN? Thanks for your advice.
Assign a single handler to the button click (MouseEvent.CLICK is the right event to use) and check the field is populated in the handler:
function clickButtonHandler(event:MouseEvent):void
{
var message:Object = new Object();
// Check the field is populated
if (txtUser.text != "")
{
message.text = txtMessage.text;
message.userName = txtUser.text;
showMessage(message);
//Posts to ALL OTHER swf files..
group.post(message);
}
else
{
// Nothing in the input field, show the alert
showAlert();
}
}
function showMessage(message:Object):void
{
output_txt.appendText(message.userName+": "+message.text + "\n");
}
function showAlert():void
{
var alert:URLRequest = new URLRequest("javascript:alert('Please enter your User name')");
navigateToURL(alert, "_self");
}

Dropdown field - first item should be blank

Using sharepoint build in lookup column and it set to required field. SharePoint automatically selects the first item in the dropdown box (kinda misleading for the end users).
Is there a way to display blank or Null for the first row of this drop down box?
(I am open to any solution. I prefer javascript type solution)
For Choice fields, the default value is configured in the column settings. If the "Default value" input box is populated, delete the value in order to use no default value.
Edit
For Lookup fields, the field seems to change dramatically if it is required. Fields that are NOT required have a "(None)" value by default. However, toggling the field to required will remove the "(None)" value and the first value is selected automatically.
One thing I found, is that if you use JavaScript to add the null value to the dropdown and then try to press OK you get an error page: "An unexpected error has occurred." As a workaround, I wrote some more code to do a quick validation that the field has a value before the form is submitted. If the field has no value, then it will prompt the user and cancel the submit. (Note: this code is only attached to the OK buttons so you may get errors while editing EditForm.aspx.. just choose a value for your lookup field and you'll be able to edit like normal)
Anyways, onto the code... I think the only line you'll need to change is var fieldTitle = 'Large Lookup Field'; to update it to the name of your field.
<script type="text/javascript">
function GetDropdownByTitle(title) {
var dropdowns = document.getElementsByTagName('select');
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns[i].title === title) {
return dropdowns[i];
}
}
return null;
}
function GetOKButtons() {
var inputs = document.getElementsByTagName('input');
var len = inputs.length;
var okButtons = [];
for (var i = 0; i < len; i++) {
if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' &&
inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
okButtons.push(inputs[i]);
}
}
return okButtons;
}
function AddValueToDropdown(oDropdown, text, value, optionnumber){
var options = oDropdown.options;
var option = document.createElement('OPTION');
option.appendChild(document.createTextNode(text));
option.setAttribute('value',value);
if (typeof(optionnumber) == 'number' && options[optionnumber]) {
oDropdown.insertBefore(option,options[optionnumber]);
}
else {
oDropdown.appendChild(option);
}
oDropdown.options.selectedIndex = 0;
}
function WrapClickEvent(element, newFunction) {
var clickFunc = element.onclick;
element.onclick = function(event){
if (newFunction()) {
clickFunc();
}
};
}
function MyCustomExecuteFunction() {
// find the dropdown
var fieldTitle = 'Large Lookup Field';
var dropdown = GetDropdownByTitle(fieldTitle);
if (null === dropdown) {
alert('Unable to get dropdown');
return;
}
AddValueToDropdown(dropdown, '', '', 0);
// add a custom validate function to the page
var funcValidate = function() {
if (0 === dropdown.selectedIndex) {
alert("Please choose a value for " + fieldTitle + ".");
// require a selection other than the first item (our blank value)
return false;
}
return true;
};
var okButtons = GetOKButtons();
for (var b = 0; b < okButtons.length; b++) {
WrapClickEvent(okButtons[b], funcValidate);
}
}
_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");
</script>
In response Kit Menke, I've made a few changes to the code so it will persist previous value of the dropdown. I have added the following lines of code to AddValueToDropdown()....
function AddValueToDropdown(oDropdown, text, value, optionnumber){
var selectedIndex
if (oDropdown.options.selectedIndex)
selectedIndex = oDropdown.options.selectedIndex;
else
selectedIndex = -1;
// original code goes here
// changed last line of code (added "selectedIndex+1")
oDropdown.options.selectedIndex = selectedIndex+1;
}
To improve on top of Aaronster's answer: AddValueToDropdown can be done that way:
var injectedBlankValue = false;
function AddValueToDropdown(oDropdown, text, value, optionnumber) {
for (i = 0; i < oDropdown.options.length; i++) {
option = oDropdown.options[i];
if(option.getAttribute('selected')) // If one is already explicitely selected: we skip the whole process
return;
}
var options = oDropdown.options;
var option = document.createElement('OPTION');
option.appendChild(document.createTextNode(text));
option.setAttribute('value', value);
if (typeof (optionnumber) == 'number' && options[optionnumber]) {
oDropdown.insertBefore(option, options[optionnumber]);
}
else {
oDropdown.appendChild(option);
}
// changed last line of code (added 'selectedIndex+1')
oDropdown.options.selectedIndex = 0;
injectedBlankValue = true;
}
This is needed for document libraries where "add" and "set properties" are two distinct pages.
And funcValidate starts with:
var funcValidate = function () {
if (!injectedBlankValue)
return true;
All these changes is to make the whole thing work with document libraries.

Resources