I try to add a copy paste option in my program but links doesn't come with.
All my blocks are well copied but my links aren't.
var copied;
$("#copy").click(function(){
var papa = block_menu.model; //clicked element
var copied_cells=papa.clone({deep:true}); //take all embedded cells
copied=graph.getSubgraph(copied_cells, {deep:true}); //copy
});
$("#paste").click(function(){
graph.addCells(copied); //paste (add on graph)
});
I've tried to add this before "copied = ...." but that doesn't change anything :
var copied_cells = graph.getSubgraph(copied_cells)
`
Does someone nows how to copy my links with?
Thanks.
cells should be sorted before you're putting them back into graph. Elements first, then links. addCells has been adding cells as it is, so if there is link whose target/source is not in the graph yet, this link won't be added.
Related
Currently retrieving images like this:
var body = DocumentApp.getActiveDocument().getBody();
var styles = {};
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var imgs = body.getImages();
imgs[0].getParent().setAttributes(styles);
Is there some attribute I can add to place line breaks before image? Or maybe get the index of image and add line breaks.
Thanks in advance.
About Is there some attribute I can add to place line breaks before image?, unfortunately, I couldn't find the attribute for directly achieving your goal. So in this case, I would like to propose maybe get the index of image and add line breaks.. When this is reflected to the script, it becomes as follows.
Sample script:
var body = DocumentApp.getActiveDocument().getBody();
var image = body.getImages()[0];
var index = body.getChildIndex(image.getParent());
body.insertParagraph(index, "\n")
When you run the script, a line break is inserted before the 1st image in the body of Google Document.
Note:
For example, when the line break is added after the inline image, you can use the script of DocumentApp.getActiveDocument().getBody().getImages()[0].getParent().asParagraph().appendText("\n").
References:
getChildIndex(child)
insertParagraph(childIndex, text)
I'm adding custom toolbar elements to a Datatables table, but I am not sure how to add more than one.
I run the JS below and it moves one element into the toolbar:
var elementOne = $('#elementOne');
$("div.toolbar").html(elementOne);
How do I add a second element to the toolbar?
I tried adding just the html of each element:
var elementOne = $('#elementOne');
var elementTwo = $('#elementTwo');
$("div.toolbar").html(elementOne.html() + elementTwo.html());
But this resulted in duplicate elements and one of them (button) did not work properly. However, I could probably fix this but I'm wondering if there's a better way.
Also, I cannot generate one of the elements within JavaScript. It must be either moved, which I'd prefer, or copied.
I figured it out. I had to use the append JQuery method:
var elementOne = $('#elementOne');
var elementTwo = $('#elementTwo');
$("div.toolbar").append(elementOne);
$("div.toolbar").append(elementTwo);
This is quite a challenging problem. I haven't seen it solved anywhere on Stack Overflow. So I decided to post it.
0 ----17----+ +---30---
| | | +----47
| | | |
<div>ABC<b>B Elem<i>Italic</i>ent</b> DEF</div>
| |
+---8--- ---37--+
Action: Let's say Element <i> tag is clicked.
Problem: Create a function that returns coordinates [17,30]
Note: The coordinates are start and end caret position, represented as 0-based index, in original HTML source code, encompassing only the element that was clicked. May assume normalized HTML nodes as in id = "" becomes id="". (But extra credit, if it doesn't.)
Example 2: If <b> tag was clicked. The script should return [8, 37] because it is the start/end caret position encompassing the B tag.
Example 3: If ABC text or DEF text was clicked, return value is [0,47]
Walk the parent chain until you hit whatever tag you consider to be a container (<div> in your case, apparently).
Use the parent's childs to locate the particular child you're coming from, in case you have two or more identical childs, like in from <i>two</i> to <i>two</i> to <i>two</i> <i>two</i>.
That should give you the child offset within the parent. You can then cumulate the offsets until you hit the div tag or whatever other container element.
Ending position is just this offset plus the clicked element length.
And after two days of solving this, I am posting my own solution.
I tried to parse the DOM and count characters manually, at first. But that was more complicated than it had to be.
Credit: Thanks to kuroi neko, who suggested the end caret position is just start position + length of the HTML encompassing the clicked tag.
Note: I am manually removing <tbody> tags, before calculating caret values. This is because, even original HTML does not contain them, during normalization process (which takes place during innerHTML or outerHTML call,) they are auto-inserted. It's a personal preference, if you're building a text editor that needs this functionality -- to leave them alone and update original HTML.
On the other hand, if you prefer the purist approach, and want to consider the original HTML intact, as it was written by the author of said HTML, then you may want to remove <tbody> manually. This also assumes that you take responsibility for taking care of all other cases, similar to these. Whatever they might be. (Not included in the solution below.)
Solution: Considering textarea (HTML source editor) and #preview are two separate elements representing the same HTML.
$(document).ready(function() {
// Normalize source code
var normalized_html = document.getElementById("preview").innerHTML;
// Remove all TBODY tags (they are auto-inserted, even if not present in original HTML)
normalized_html = normalized_html.replace(/<tbody>/g, '');
$("#textarea").html(normalized_html);
$("#preview").on("click", function(event) {
// Get clicked tag HTML
var tag = event.target.outerHTML;
// Get original HTML before split character is inserted
var orig_html = document.getElementById("preview").innerHTML;//.replace(/<preview>/g, '').replace(/<\/preview>/g, '');
// Insert unique separator just before the tag that was clicked, to mark beginning
$(event.target).before("[*-*]");
// Get preview source code
var html = document.getElementById("preview").innerHTML;
// Remove line breaks
html = html.replace(/\r|\n/g, '');
// Remove tags that were auto-inserted by native normalization process that did not exist in original HTML.
html = html.replace(/<tbody>/g, '');
var before_split = html;
// Split HTML at the tag that was clicked
html = html.split("[*-*]")[0];
// Restore preview to original HTML
$("#preview")[0].innerHTML = orig_html;
// Get start and end of caret in source code
var caret_start = html.length;
var caret_end = caret_start + tag.length;
console.log("caret start = " + caret_start + " end = " + caret_end);
});
});
You achieve that by simply using Descop library.
// Get the source html code of target document
var html = yourFunctionToGetHTML();
// Get the target document itself
var dom = yourFunctionToGetDocument();
// Get the element you want to found in source code
var element = document.getElementById("target-element");
// Create an instance of Descop
var descop = new Descop();
// Connect document
descop.connectDocument(dom);
// Connect source code
descop.connectSource(html);
// Get element position in source code
var position = descop.getElementPosition(element);
// eg. position => { start: 320, end: 480 }
I am making a simple web app. In one part of it, I have a dynamically generated list:
which is achieved with:
for(var i=0; i<goalsObj.length; i++){
var node = document.createElement("li");
node.setAttribute("class", "list");
node.setAttribute('id','g'+i);
var checkbox = document.createElement("input");
checkbox.setAttribute("type","checkbox");
checkbox.setAttribute("class", "tickbox");
node.appendChild(checkbox);
var textnode = document.createTextNode(goalsObj[i]);
node.appendChild(textnode);
document.getElementById("sortable").appendChild(node);
}
And I have a jQuery function executed when any item on the list is clicked, to app a Calendar below it.
which is achieved with:
var cal = document.createElement("p");
cal.innerHTML=calendar_html;
document.getElementById($(this).attr('id')).appendChild(cal);
Anyhow, I am getting a very shabby output:
What's wrong? What should I do? How do I make the pre-existing elements(all made dynamically) to make way for the newly created ones?
Generate the whole content at once and not in parts. Hide the content that you do not want on initialization of the page. Write a function to show the hidden content when the list items are clicked.
Paragraph ("p" element) is for organization of information into paragraphs.
http://www.w3.org/TR/html401/struct/text.html#h-9.3
I suppose you should try to use div instead of p
var cal = document.createElement("div");
cal.innerHTML=calendar_html;
I have a JS function that copies an image from a div onto a clipboard (only in IE) and pastes it into Excel. I would like to know how I can paste the contents of the clipboard into a specific cell in Excel. Here is the JS function code snippet I have. I would like to know how I can paste the clipboard contents into a specific cell
var imgObj = document.getElementById('exportimage');
imgObj.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange)
{
controlRange = document.body.createControlRange();
controlRange.addElement(imgObj);
controlRange.execCommand('Copy');
var objExcel = new ActiveXObject ("Excel.Application");
objExcel.visible = true;
var objWorkbook = objExcel.Workbooks.Add;
var objWorksheet = objWorkbook.Worksheets(1);
objWorksheet.Paste;
}
Copying to clipboard from div, and pasting it into Excel may not be a recommended approach but I would like to get this working for an internal (use only) application
The best you will achieve is pasting to the location where the cell is. You can't insert into the cell. There are other things you may want to do such as insert the image into a comment, or paste an image link, but actually placing the image in the cell isn't going to work.
The closest you can get is to define the Range("A1").Select (or whatever location) and paste to that beginning point. You can also fine tune the location however, so depending on what you are trying to do you can offset from a cell range by specific amounts, if that is what you need.