I am trying to build a program that will take a user input in a text box and display it. My problem is that when I enter a new input into the text box hat input overlaps the first text. What I want is that original text to be removed and replaced by the new input. The code I am using is below.
function eccentricityChanged() {
let svg = document.getElementById('diagram');
let text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', 585);
text.setAttribute('y', 275);
text.setAttribute('fill', 'red');
svg.appendChild(text);
svg.selectAll('*').remove();
let txt = document.getElementById("eccentricity").value;
parseFloat('text', txt);
text.innerHTML = txt;
Related
I have a PowerPoint template with placeholder data. I need to swap out the placeholder text with some numbers using Node, but I'm having trouble finding a package that supports this. Has anyone seen anything along these lines?
Have you looked into the PowerPoint JavaScript API?
For example:
Call the ShapeCollection.getItem(key) method to get your Shape object
Update the text value via Shape.textFrame.textRange.text
Related example from Microsoft's docs:
// This sample creates a light blue rectangle with braces ("{}") on the left and right ends
// and adds the purple text "Shape text" to the center.
await PowerPoint.run(async (context) => {
const shapes = context.presentation.slides.getItemAt(0).shapes;
const braces = shapes.addGeometricShape(PowerPoint.GeometricShapeType.bracePair);
braces.left = 100;
braces.top = 400;
braces.height = 50;
braces.width = 150;
braces.name = "Braces";
braces.fill.setSolidColor("lightblue");
braces.textFrame.textRange.text = "Shape text";
braces.textFrame.textRange.font.color = "purple";
braces.textFrame.verticalAlignment = PowerPoint.TextVerticalAlignment.middleCentered;
await context.sync();
});
I'm using fabric.js and have IText textboxes on canvas. The user can freely move the textbox around and also edit the text within. How to avoid a textbox being deleted (removed from the canvas) when the user deletes the text using delete key and there's no more text?
Thanks for the response.
deleteText(activeObject) {
var startIndex = activeObject.selectionStart;
var endIndex = activeObject.selectionEnd;
if (endIndex > 0 && startIndex != endIndex) {
var text = activeObject.text;
var newText =
text.substring(0, startIndex) +
text.substring(endIndex + 1, text.length);
activeObject.set("text", newText);
this.canvas.fire("object:modified");
} else {
this.canvas.remove(activeObject);
}
}
have 2 versions of answer
1.Your are deleting textbox using this line of code , try commenting it and see what happens
this.canvas.remove(activeObject);
2.Textbox is not being deleted , you can't see it because of white canvas background and emptiness inside textbox, since there is no text , try mousedowning a canvas and select a big area , check if you are able to select your textbox this way
I'm quite puzzled with how strings are handled in Google Script. In particular, it seems that strings can be styled, but I have no idea how to actually do this.
For instance: I create a Google Form, add a Short Text question, and copy paste a bold text generated here : https://lingojam.com/BoldTextGenerator
If I open this form with a script, I can recover this text with
var form = FormApp.openById(formID);
var Items = form.getItems();
var test = Items[0].getTitle();
This "test" variable is a string (I checked with Logger.log(typeof(test))), not a "Text" nor "RichText" and methods such as .isBold() won't work.
However, Logger.log(test) does output a bold text in the log journal - so this string does contain some information about its style.
Yet, I can't seem to define a styled string within Google Script. I have tried quite different things, but none worked
var dummy = "Hello World !"
Logger.log(dummy.bold())
Logger.log(dummy.setBold(true))
Logger.log(dummy.setFontWeight("bold"))
Logger.log("<b>"+dummy+"</b>")
Logger.log("**"+dummy+"**")
What can I do to have my dummy string to be logged in a bold font (my real objective being to use a .setTitle(dummy) method to have a bold font form item) ?
I believe your goal as follows.
You want to set the text with the bold type to the title of items on Google Form using Google Apps Script.
Issue and workaround:
Unfortunately, in the current stage, there are not methods for directly managing the rich texts to the title of each items on Google Form in Google Form service. But when the text with the bold type is directly copied and pasted to the title of item on Google Form, it can be done. So in this answer, using this, as a current workaround, I would like to propose to convert the text data to the text with the bold type with the unicode, and put the converted text to Google Form.
The flow of this workaround is as follows.
Convert the text to the bold type with the unicode.
In this conversion, each character in the text is converted to the bold type using the difference between the original character code and the bold character code.
Put to the converted text to the title of item on Google Form.
When above flow is reflected to the script, it becomes as follows.
Sample script:
function myFunction() {
// 1. Convert the text to the bold type with the unicode.
const conv = {
c: function(text, obj) {return text.replace(new RegExp(`[${obj.reduce((s, {r}) => s += r, "")}]`, "g"), e => {
const t = e.codePointAt(0);
if ((t >= 48 && t <= 57) || (t >= 65 && t <= 90) || (t >= 97 && t <= 122)) {
return obj.reduce((s, {r, d}) => {
if (new RegExp(`[${r}]`).test(e)) s = String.fromCodePoint(e.codePointAt(0) + d);
return s;
}, "")
}
return e;
})},
bold: function(text) {return this.c(text, [{r: "0-9", d: 120734}, {r: "A-Z", d: 120211}, {r: "a-z", d: 120205}])},
italic: function(text) {return this.c(text, [{r: "A-Z", d: 120263}, {r: "a-z", d: 120257}])},
boldItalic: function(text) {return this.c(text, [{r: "A-Z", d: 120315}, {r: "a-z", d: 120309}])},
};
var newTitle = "New title for item 1";
var convertedNewTitle = conv.bold(newTitle); // Bold type
// var convertedNewTitle = conv.italic(newTitle); // Italic type
// var convertedNewTitle = conv.boldItalic(newTitle); // Bold-italic type
// 2. Put to the converted text to the title of item on Google Form.
var formID = "###"; // Please set the Form ID.
var form = FormApp.openById(formID);
var Items = form.getItems();
Items[0].setTitle(convertedNewTitle);
}
In this sample script, the bold, italic and bold-italic types can be converted.
In this case, the numbers and the specific characters have no bold, italic and bold-italic types. Please be careful this.
Result:
When above sample script is used, the following result is obtained.
From:
To:
Testing
https://jsfiddle.net/7bL5r3em/
References:
Unicode font
https://lingojam.com/BoldTextGenerator
This is from your question.
I want to align the text in a TextArea to the right. I tried the following code:
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(RIGHT);
form.addComponent(textArea);
The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:
So how to align the content to the RIGHT ?
It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(TextArea.LEFT);
form.addComponent(textArea);
Setting it to LEFT makes the displayed text RIGHT aligned !
Or by removing the textArea.setRTL(true) which is mirroring the display
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setAlignment(TextArea.RIGHT);
form.addComponent(textArea);
For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}
And drawTextArea method in DefaultLookAndFeel is as follows:
int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
// remaining code is here in initial source
}
g.drawString(displayText, x, y);
Unfortunately TextArea.RIGHT value is 3
But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)
Meanwhile TextArea.Left value is 1
That's why it matched the value in the switch and was aligned to RIGHT
BTW, if you set
textArea.setAlignment(Component.RIGHT);
it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !
You only have to write 'TextArea.RIGHT' instead of 'RIGHT'
textArea.setAlignment(TextArea.RIGHT);
You can use the following line:
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
I'm looking for a way to extract images from an ID file in a 'formatted' / cropped form.
i.e: a. I have placed numerous, hi-res (tiff, psd) images into an InDesign CS5 file
b. The image boxes that they have been placed into, are smaller than the actual image (pretty intense cropping occurred) c. I am trying to collect these images in their new stage (cropped to the image box) and export them as jpg at 72dpi.
Are there any plug-ins out there that would automatically collect "formatted" images from ID for me? Or is there some other way?
If you're familiar with Indesign Scripting, this can very easily be done via a script. I use Javascript but this can be done with VBSript or AppleScript as well. Here is a basic example of a script that will open a document, and export a rectangle (your image box) as a JPG. Basically, you can just loop through the pictures in your document and export each one to a location/filename you choose (see the myFile variable below). There are several "jpegExportPreferences" you can pick from to determine how your output JPG will be (i.e. DPI).
test();
function test(){
var myDoc = app.open('c:/user/desktop/testDocument.indd');
var myGroups = myDoc.groups;
//for each group...
for (var i = 0;i < myGroups.length; i++){
// for each rectangle in the group...
for(var r = 0; r< myGroups[i].rectangles.length; r++){
var myRect = myGroups[i].rectangles[r];
app.jpegExportPreferences.exportResolution = 300;
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
//give it a unique name
var myFile = new File('c:/users/desktop/newJPG' + myRect.id + '.jpg');
myRect.exportFile(ExportFormat.JPG, myFile);
}
}
}
For a list of the other optional JPG Export preferences, see this link:
http://indesignscriptingreference.com/cs2/javascript-cs2/jpegexportpreference.htm
Hope this helps!